input()
C = [[x[0], x[1]] for x in list(input().split())]
D = C.copy()
N = len(C)

# バブルソート
for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
        if C[j][1] < C[j-1][1]:
            tmp = C[j]
            C[j] = C[j-1]
            C[j-1] = tmp

# 選択ソート
for i in range(0, N-1, 1):
    minj = i
    for j in range(i, N, 1):
        if D[j][1] < D[minj][1]:
            minj = j
    tmp = D[i]
    D[i] = D[minj]
    D[minj] = tmp

# バブルソートの結果出力
length = len(C)
for i in range(length):
    if i == length - 1:
        print("".join(C[i]), end="")
    else:
        print("".join(C[i]), end=" ")

print("")
print("Stable")

# 選択ソートの結果出力
for i in range(length):
    if i == length - 1:
        print("".join(D[i]), end="")
    else:
        print("".join(D[i]), end=" ")

print("")
if C == D:
    print("Stable")
else:
    print("Not stable")
 input() input [x[0], x[1]] for x in list(input().split()) for x in list(input().split()) x list(input().split()) list input().split() ().split () input split for x in list(input().split()) [x[0], x[1]] x[0] x 0 x[1] x 1 C = [[x[0], x[1]] for x in list(input().split())] C [[x[0], x[1]] for x in list(input().split())] D = C.copy() D C.copy() C.copy C copy N = len(C) N len(C) len C for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
        if C[j][1] < C[j-1][1]:
            tmp = C[j]
            C[j] = C[j-1]
            C[j-1] = tmp

# 選択ソート i range(0, N-1, 1) range 0 N-1 N 1 1 for j in range(N-1, i, -1):
        if C[j][1] < C[j-1][1]:
            tmp = C[j]
            C[j] = C[j-1]
            C[j-1] = tmp

# 選択ソート j range(N-1, i, -1) range N-1 N 1 i -1 if C[j][1] < C[j-1][1]:
            tmp = C[j]
            C[j] = C[j-1]
            C[j-1] = tmp

# 選択ソート C[j][1] < C[j-1][1] C[j][1] [j] C j 1 C[j-1][1] [j-1] C j-1 j 1 1 tmp = C[j] tmp C[j] C j C[j] = C[j-1] C[j] C j C[j-1] C j-1 j 1 C[j-1] = tmp C[j-1] C j-1 j 1 tmp for i in range(0, N-1, 1):
    minj = i
    for j in range(i, N, 1):
        if D[j][1] < D[minj][1]:
            minj = j
    tmp = D[i]
    D[i] = D[minj]
    D[minj] = tmp

# バブルソートの結果出力 i range(0, N-1, 1) range 0 N-1 N 1 1 minj = i minj i for j in range(i, N, 1):
        if D[j][1] < D[minj][1]:
            minj = j
     j range(i, N, 1) range i N 1 if D[j][1] < D[minj][1]:
            minj = j
     D[j][1] < D[minj][1] D[j][1] [j] D j 1 D[minj][1] [minj] D minj 1 minj = j minj j tmp = D[i] tmp D[i] D i D[i] = D[minj] D[i] D i D[minj] D minj D[minj] = tmp D[minj] D minj tmp length = len(C) length len(C) len C for i in range(length):
    if i == length - 1:
        print("".join(C[i]), end="")
    else:
        print("".join(C[i]), end=" ") i range(length) range length if i == length - 1:
        print("".join(C[i]), end="")
    else:
        print("".join(C[i]), end=" ") i == length - 1 i length - 1 length 1 print("".join(C[i]), end="") print "".join(C[i]) "".join "" join C[i] C i end="" end "" print("".join(C[i]), end=" ") print "".join(C[i]) "".join "" join C[i] C i end=" " end " " print("") print "" print("Stable") print "Stable" for i in range(length):
    if i == length - 1:
        print("".join(D[i]), end="")
    else:
        print("".join(D[i]), end=" ") i range(length) range length if i == length - 1:
        print("".join(D[i]), end="")
    else:
        print("".join(D[i]), end=" ") i == length - 1 i length - 1 length 1 print("".join(D[i]), end="") print "".join(D[i]) "".join "" join D[i] D i end="" end "" print("".join(D[i]), end=" ") print "".join(D[i]) "".join "" join D[i] D i end=" " end " " print("") print "" if C == D:
    print("Stable")
else:
    print("Not stable") C == D C D print("Stable") print "Stable" print("Not stable") print "Not stable" C = [[x[0], x[1]] for x in list(input().split())] [[x[0], x[1]] for x in list(input().split())] C minj = i i minj minj = j j minj D = C.copy() C.copy() D tmp = C[j] C[j] tmp tmp = D[i] D[i] tmp N = len(C) len(C) N length = len(C) len(C) length
input()
C = [[x[0], x[1]] for x in list(input().split())]
D = C.copy()
N = len(C)

# バブルソート
for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
        if C[j][1] < C[j-1][1]:
            tmp = C[j]
            C[j] = C[j-1]
            C[j-1] = tmp

# 選択ソート
for i in range(0, N-1, 1):
    minj = i
    for j in range(i, N, 1):
        if D[j][1] < D[minj][1]:
            minj = j
    tmp = D[i]
    D[i] = D[minj]
    D[minj] = tmp

# バブルソートの結果出力
length = len(C)
for i in range(length):
    if i == length - 1:
        print("".join(C[i]), end="")
    else:
        print("".join(C[i]), end=" ")

print("")
print("Stable")

# 選択ソートの結果出力
for i in range(length):
    if i == length - 1:
        print("".join(D[i]), end="")
    else:
        print("".join(D[i]), end=" ")

print("")
if C == D:
    print("Stable")
else:
    print("Not stable")

