import copy

n = int(input())

l = list(input().split())
C1 = []

for v in l:
    s = v[0]
    m = int(v[1])
    C1.append((s, m))

C2 = copy.deepcopy(C1)

for i in range(n):
    for j in range(n-1, i, -1):
        if C1[j][1] < C1[j-1][1]:
            C1[j], C1[j-1] = C1[j-1], C1[j]

for i in range(n):
    minj = i
    for j in range(i,n):
        if C2[j][1] < C2[minj][1]:
            minj = j
    C2[i], C2[minj] = C2[minj], C2[i]

result1 = [t[0]+str(t[1]) for  t in C1]
result2 = [t[0]+str(t[1]) for  t in C2]

print(" ".join(result1))
print("Stable")
print(" ".join(result2))
if result1 == result2:
    print("Stable")
else:
    print("Not stable")
 import copy copy n = int(input()) n int(input()) int input() input l = list(input().split()) l list(input().split()) list input().split() ().split () input split C1 = [] C1 [] for v in l:
    s = v[0]
    m = int(v[1])
    C1.append((s, m)) v l s = v[0] s v[0] v 0 m = int(v[1]) m int(v[1]) int v[1] v 1 C1.append((s, m)) C1.append C1 append (s, m) s m C2 = copy.deepcopy(C1) C2 copy.deepcopy(C1) copy.deepcopy copy deepcopy C1 for i in range(n):
    for j in range(n-1, i, -1):
        if C1[j][1] < C1[j-1][1]:
            C1[j], C1[j-1] = C1[j-1], C1[j] i range(n) range n for j in range(n-1, i, -1):
        if C1[j][1] < C1[j-1][1]:
            C1[j], C1[j-1] = C1[j-1], C1[j] j range(n-1, i, -1) range n-1 n 1 i -1 if C1[j][1] < C1[j-1][1]:
            C1[j], C1[j-1] = C1[j-1], C1[j] C1[j][1] < C1[j-1][1] C1[j][1] [j] C1 j 1 C1[j-1][1] [j-1] C1 j-1 j 1 1 C1[j], C1[j-1] = C1[j-1], C1[j] C1[j] C1 j C1[j-1] C1 j-1 j 1 C1[j-1] C1 j-1 j 1 C1[j] C1 j for i in range(n):
    minj = i
    for j in range(i,n):
        if C2[j][1] < C2[minj][1]:
            minj = j
    C2[i], C2[minj] = C2[minj], C2[i] i range(n) range n minj = i minj i for j in range(i,n):
        if C2[j][1] < C2[minj][1]:
            minj = j
     j range(i,n) range i n if C2[j][1] < C2[minj][1]:
            minj = j
     C2[j][1] < C2[minj][1] C2[j][1] [j] C2 j 1 C2[minj][1] [minj] C2 minj 1 minj = j minj j C2[i], C2[minj] = C2[minj], C2[i] C2[i] C2 i C2[minj] C2 minj C2[minj] C2 minj C2[i] C2 i t[0]+str(t[1]) for  t in C1 for  t in C1 t C1 for  t in C1 t[0]+str(t[1]) t[0] t 0 str(t[1]) str t[1] t 1 result1 = [t[0]+str(t[1]) for  t in C1] result1 [t[0]+str(t[1]) for  t in C1] t[0]+str(t[1]) for  t in C2 for  t in C2 t C2 for  t in C2 t[0]+str(t[1]) t[0] t 0 str(t[1]) str t[1] t 1 result2 = [t[0]+str(t[1]) for  t in C2] result2 [t[0]+str(t[1]) for  t in C2] print(" ".join(result1)) print " ".join(result1) " ".join " " join result1 print("Stable") print "Stable" print(" ".join(result2)) print " ".join(result2) " ".join " " join result2 if result1 == result2:
    print("Stable")
else:
    print("Not stable") result1 == result2 result1 result2 print("Stable") print "Stable" print("Not stable") print "Not stable" C2 = copy.deepcopy(C1) copy.deepcopy(C1) C2 m = int(v[1]) int(v[1]) m s = v[0] v[0] s result2 = [t[0]+str(t[1]) for  t in C2] [t[0]+str(t[1]) for  t in C2] result2 l = list(input().split()) list(input().split()) l C1 = [] [] C1 result1 = [t[0]+str(t[1]) for  t in C1] [t[0]+str(t[1]) for  t in C1] result1 n = int(input()) int(input()) n minj = j j minj minj = i i minj
import copy

n = int(input())

l = list(input().split())
C1 = []

for v in l:
    s = v[0]
    m = int(v[1])
    C1.append((s, m))

C2 = copy.deepcopy(C1)

for i in range(n):
    for j in range(n-1, i, -1):
        if C1[j][1] < C1[j-1][1]:
            C1[j], C1[j-1] = C1[j-1], C1[j]

for i in range(n):
    minj = i
    for j in range(i,n):
        if C2[j][1] < C2[minj][1]:
            minj = j
    C2[i], C2[minj] = C2[minj], C2[i]

result1 = [t[0]+str(t[1]) for  t in C1]
result2 = [t[0]+str(t[1]) for  t in C2]

print(" ".join(result1))
print("Stable")
print(" ".join(result2))
if result1 == result2:
    print("Stable")
else:
    print("Not stable")

