def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x

def selection_sort(c, n):
    x = c[:]
    for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
    return x

n = int(input())
c = []
for i in list(input().split()):
    c.append((i[0], int(i[1]))) # tupleで渡す

b = bubble_sort(c, n)
print(" ".join(map(str, [i[0]+str(i[1]) for i in b])))
# bubble_sortは常に安定なソート
stable = True
print("Stable")

s = selection_sort(c, n)
print(" ".join(map(str, [i[0]+str(i[1]) for i in s])))
if b != s:
    stable = False
if stable:
    print("Stable")
else:
    print("Not stable") def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x bubble_sort x = c[:] x c[:] c : for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
     i range(n) range n for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
     k range(n-1, i, -1) range n-1 n 1 i -1 if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
     x[k][1] < x[k-1][1] x[k][1] [k] x k 1 x[k-1][1] [k-1] x k-1 k 1 1 x[k], x[k-1] = x[k-1], x[k] x[k] x k x[k-1] x k-1 k 1 x[k-1] x k-1 k 1 x[k] x k return x x c c n n def selection_sort(c, n):
    x = c[:]
    for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
    return x selection_sort x = c[:] x c[:] c : for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
     i range(n) range n mink = i mink i for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
         k range(i, n) range i n if x[k][1] < x[mink][1]:
                mink = k
         x[k][1] < x[mink][1] x[k][1] [k] x k 1 x[mink][1] [mink] x mink 1 mink = k mink k x[i], x[mink] = x[mink], x[i] x[i] x i x[mink] x mink x[mink] x mink x[i] x i return x x c c n n n = int(input()) n int(input()) int input() input c = [] c [] for i in list(input().split()):
    c.append((i[0], int(i[1]))) # tupleで渡す i list(input().split()) list input().split() ().split () input split c.append((i[0], int(i[1]))) c.append c append (i[0], int(i[1])) i[0] i 0 int(i[1]) int i[1] i 1 b = bubble_sort(c, n) b bubble_sort(c, n) bubble_sort c n i[0]+str(i[1]) for i in b for i in b i b for i in b i[0]+str(i[1]) i[0] i 0 str(i[1]) str i[1] i 1 print(" ".join(map(str, [i[0]+str(i[1]) for i in b]))) print " ".join(map(str, [i[0]+str(i[1]) for i in b])) " ".join " " join map(str, [i[0]+str(i[1]) for i in b]) map str [i[0]+str(i[1]) for i in b] stable = True stable True print("Stable") print "Stable" s = selection_sort(c, n) s selection_sort(c, n) selection_sort c n i[0]+str(i[1]) for i in s for i in s i s for i in s i[0]+str(i[1]) i[0] i 0 str(i[1]) str i[1] i 1 print(" ".join(map(str, [i[0]+str(i[1]) for i in s]))) print " ".join(map(str, [i[0]+str(i[1]) for i in s])) " ".join " " join map(str, [i[0]+str(i[1]) for i in s]) map str [i[0]+str(i[1]) for i in s] if b != s:
    stable = False b != s b s stable = False stable False if stable:
    print("Stable")
else:
    print("Not stable") stable print("Stable") print "Stable" print("Not stable") print "Not stable" c = [] [] c b = bubble_sort(c, n) bubble_sort(c, n) b n = int(input()) int(input()) n stable = False False stable def selection_sort(c, n):
    x = c[:]
    for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
    return x def selection_sort(c, n):
    x = c[:]
    for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
    return x selection_sort stable = True True stable def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x bubble_sort s = selection_sort(c, n) selection_sort(c, n) s
def bubble_sort(c, n):
    x = c[:]
    for i in range(n):
        for k in range(n-1, i, -1):
            if x[k][1] < x[k-1][1]:
                x[k], x[k-1] = x[k-1], x[k]
    return x

def selection_sort(c, n):
    x = c[:]
    for i in range(n):
        mink = i
        for k in range(i, n):
            if x[k][1] < x[mink][1]:
                mink = k
        x[i], x[mink] = x[mink], x[i]
    return x

n = int(input())
c = []
for i in list(input().split()):
    c.append((i[0], int(i[1]))) # tupleで渡す

b = bubble_sort(c, n)
print(" ".join(map(str, [i[0]+str(i[1]) for i in b])))
# bubble_sortは常に安定なソート
stable = True
print("Stable")

s = selection_sort(c, n)
print(" ".join(map(str, [i[0]+str(i[1]) for i in s])))
if b != s:
    stable = False
if stable:
    print("Stable")
else:
    print("Not stable")
