class Card:
    def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i

def BubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

    return C


n = int(input())
cards = [Card(c, i) for (i, c) in enumerate(input().split())]

res_bub = BubbleSort(cards[:], n)
res_sel = SelectionSort(cards[:], n)

print(*[c.suit + c.value for c in res_bub])

for i in range(n - 1):
    if res_bub[i].value == res_bub[i + 1].value:
        if res_bub[i].iniorder > res_bub[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable")

print(*[c.suit + c.value for c in res_sel])

for i in range(n - 1):
    if res_sel[i].value == res_sel[i + 1].value:
        if res_sel[i].iniorder > res_sel[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable") class Card:
    def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i Card def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i __init__ self.suit = s[0] self.suit self suit s[0] s 0 self.value = s[1] self.value self value s[1] s 1 self.iniorder = i self.iniorder self iniorder i self self s s i i def BubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

    return C BubbleSort for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

     i range(N) range N for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

     j range(N - 1, i, -1) range N - 1 N 1 i -1 if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

     C[j].value < C[j - 1].value C[j].value [j] C j value C[j - 1].value [j - 1] C j - 1 j 1 value C[j], C[j - 1] = C[j - 1], C[j] C[j] C j C[j - 1] C j - 1 j 1 C[j - 1] C j - 1 j 1 C[j] C j return C C C C N N def SelectionSort(C, N):
    for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

    return C SelectionSort for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

     i range(N) range N minj = i minj i for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

         j range(i, N) range i N if C[j].value < C[minj].value:
                minj = j

         C[j].value < C[minj].value C[j].value [j] C j value C[minj].value [minj] C minj value minj = j minj j C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i return C C C C N N n = int(input()) n int(input()) int input() input Card(c, i) for (i, c) in enumerate(input().split()) for (i, c) in enumerate(input().split()) (i, c) i c enumerate(input().split()) enumerate input().split() ().split () input split for (i, c) in enumerate(input().split()) Card(c, i) Card c i cards = [Card(c, i) for (i, c) in enumerate(input().split())] cards [Card(c, i) for (i, c) in enumerate(input().split())] res_bub = BubbleSort(cards[:], n) res_bub BubbleSort(cards[:], n) BubbleSort cards[:] cards : n res_sel = SelectionSort(cards[:], n) res_sel SelectionSort(cards[:], n) SelectionSort cards[:] cards : n c.suit + c.value for c in res_bub for c in res_bub c res_bub for c in res_bub c.suit + c.value c.suit c suit c.value c value print(*[c.suit + c.value for c in res_bub]) print *[c.suit + c.value for c in res_bub] [c.suit + c.value for c in res_bub] for i in range(n - 1):
    if res_bub[i].value == res_bub[i + 1].value:
        if res_bub[i].iniorder > res_bub[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable") i range(n - 1) range n - 1 n 1 if res_bub[i].value == res_bub[i + 1].value:
        if res_bub[i].iniorder > res_bub[i + 1].iniorder:
            print("Not stable")
            break res_bub[i].value == res_bub[i + 1].value res_bub[i].value [i] res_bub i value res_bub[i + 1].value [i + 1] res_bub i + 1 i 1 value if res_bub[i].iniorder > res_bub[i + 1].iniorder:
            print("Not stable")
            break res_bub[i].iniorder > res_bub[i + 1].iniorder res_bub[i].iniorder [i] res_bub i iniorder res_bub[i + 1].iniorder [i + 1] res_bub i + 1 i 1 iniorder print("Not stable") print "Not stable" break print("Stable") print "Stable" c.suit + c.value for c in res_sel for c in res_sel c res_sel for c in res_sel c.suit + c.value c.suit c suit c.value c value print(*[c.suit + c.value for c in res_sel]) print *[c.suit + c.value for c in res_sel] [c.suit + c.value for c in res_sel] for i in range(n - 1):
    if res_sel[i].value == res_sel[i + 1].value:
        if res_sel[i].iniorder > res_sel[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable") i range(n - 1) range n - 1 n 1 if res_sel[i].value == res_sel[i + 1].value:
        if res_sel[i].iniorder > res_sel[i + 1].iniorder:
            print("Not stable")
            break res_sel[i].value == res_sel[i + 1].value res_sel[i].value [i] res_sel i value res_sel[i + 1].value [i + 1] res_sel i + 1 i 1 value if res_sel[i].iniorder > res_sel[i + 1].iniorder:
            print("Not stable")
            break res_sel[i].iniorder > res_sel[i + 1].iniorder res_sel[i].iniorder [i] res_sel i iniorder res_sel[i + 1].iniorder [i + 1] res_sel i + 1 i 1 iniorder print("Not stable") print "Not stable" break print("Stable") print "Stable" res_sel = SelectionSort(cards[:], n) SelectionSort(cards[:], n) res_sel class Card:
    def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i class Card:
    def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i Card def BubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

    return C def BubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

    return C BubbleSort def SelectionSort(C, N):
    for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

    return C def SelectionSort(C, N):
    for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

    return C SelectionSort res_bub = BubbleSort(cards[:], n) BubbleSort(cards[:], n) res_bub cards = [Card(c, i) for (i, c) in enumerate(input().split())] [Card(c, i) for (i, c) in enumerate(input().split())] cards n = int(input()) int(input()) n
class Card:
    def __init__(self, s, i):
        self.suit = s[0]
        self.value = s[1]
        self.iniorder = i

def BubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j].value < C[j - 1].value:
                C[j], C[j - 1] = C[j - 1], C[j]

    return C

def SelectionSort(C, N):
    for i in range(N):
        minj = i

        for j in range(i, N):
            if C[j].value < C[minj].value:
                minj = j

        C[i], C[minj] = C[minj], C[i]

    return C


n = int(input())
cards = [Card(c, i) for (i, c) in enumerate(input().split())]

res_bub = BubbleSort(cards[:], n)
res_sel = SelectionSort(cards[:], n)

print(*[c.suit + c.value for c in res_bub])

for i in range(n - 1):
    if res_bub[i].value == res_bub[i + 1].value:
        if res_bub[i].iniorder > res_bub[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable")

print(*[c.suit + c.value for c in res_sel])

for i in range(n - 1):
    if res_sel[i].value == res_sel[i + 1].value:
        if res_sel[i].iniorder > res_sel[i + 1].iniorder:
            print("Not stable")
            break
else:
    print("Stable")