import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LS()
A_Bub = []
A_Sel = []
for i in range(N):
    A_Bub.append([A[i][0], int(A[i][1]), i])
    A_Sel.append([A[i][0], int(A[i][1]), i])

#BubleSort(C, N)
def BubleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N)
def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a

def Check(C, N):
    for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
    return 'Stable'
BubleSort(A_Bub, N)
ans_Bub = []
for i in range(N):
    b = [A_Bub[i][0], A_Bub[i][1]]
    ans_Bub.append(''.join(map(str, b)))
print(' '.join(map(str, ans_Bub)))
print(Check(A_Bub, N))


SelectionSort(A_Sel, N)
ans_Sel = []
for i in range(N):
    s = [A_Sel[i][0], A_Sel[i][1]]
    ans_Sel.append(''.join(map(str, s)))
print(' '.join(map(str, ans_Sel)))
print(Check(A_Sel, N)) import bisect, collections, copy, heapq, itertools, math, string bisect collections copy heapq itertools math string import sys sys def I(): return int(sys.stdin.readline().rstrip()) I def MI(): return map(int, sys.stdin.readline().rstrip().split()) MI def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) LI def S(): return sys.stdin.readline().rstrip() S def LS(): return list(sys.stdin.readline().rstrip().split()) LS N = I() N I() I A = LS() A LS() LS A_Bub = [] A_Bub [] A_Sel = [] A_Sel [] for i in range(N):
    A_Bub.append([A[i][0], int(A[i][1]), i])
    A_Sel.append([A[i][0], int(A[i][1]), i])

#BubleSort(C, N) i range(N) range N A_Bub.append([A[i][0], int(A[i][1]), i]) A_Bub.append A_Bub append [A[i][0], int(A[i][1]), i] A[i][0] [i] A i 0 int(A[i][1]) int A[i][1] [i] A i 1 i A_Sel.append([A[i][0], int(A[i][1]), i]) A_Sel.append A_Sel append [A[i][0], int(A[i][1]), i] A[i][0] [i] A i 0 int(A[i][1]) int A[i][1] [i] A i 1 i def BubleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) BubleSort for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) i range(N) range N for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) j range(N - 1, i, -1) range N - 1 N 1 i -1 if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) 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 a = C[j] a C[j] C j b = C[j - 1] b C[j - 1] C j - 1 j 1 C[j] = b C[j] C j b C[j - 1] = a C[j - 1] C j - 1 j 1 a C C N N def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a SelectionSort for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a i range(N) range N minj = i minj i for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
         j range(i, N) range i N if C[j][1] < C[minj][1]:
                minj = j
         C[j][1] < C[minj][1] C[j][1] [j] C j 1 C[minj][1] [minj] C minj 1 minj = j minj j a = C[i] a C[i] C i b = C[minj] b C[minj] C minj C[i] = b C[i] C i b C[minj] = a C[minj] C minj a C C N N def Check(C, N):
    for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
    return 'Stable' Check for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
     i range(1, N) range 1 N if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
     C[i][1] == C[i - 1][1] C[i][1] [i] C i 1 C[i - 1][1] [i - 1] C i - 1 i 1 1 if C[i][2] < C[i - 1][2]:
                return 'Not stable'
     C[i][2] < C[i - 1][2] C[i][2] [i] C i 2 C[i - 1][2] [i - 1] C i - 1 i 1 2 return 'Not stable' 'Not stable' return 'Stable' 'Stable' C C N N BubleSort(A_Bub, N) BubleSort A_Bub N ans_Bub = [] ans_Bub [] for i in range(N):
    b = [A_Bub[i][0], A_Bub[i][1]]
    ans_Bub.append(''.join(map(str, b))) i range(N) range N b = [A_Bub[i][0], A_Bub[i][1]] b [A_Bub[i][0], A_Bub[i][1]] A_Bub[i][0] [i] A_Bub i 0 A_Bub[i][1] [i] A_Bub i 1 ans_Bub.append(''.join(map(str, b))) ans_Bub.append ans_Bub append ''.join(map(str, b)) ''.join '' join map(str, b) map str b print(' '.join(map(str, ans_Bub))) print ' '.join(map(str, ans_Bub)) ' '.join ' ' join map(str, ans_Bub) map str ans_Bub print(Check(A_Bub, N)) print Check(A_Bub, N) Check A_Bub N SelectionSort(A_Sel, N) SelectionSort A_Sel N ans_Sel = [] ans_Sel [] for i in range(N):
    s = [A_Sel[i][0], A_Sel[i][1]]
    ans_Sel.append(''.join(map(str, s))) i range(N) range N s = [A_Sel[i][0], A_Sel[i][1]] s [A_Sel[i][0], A_Sel[i][1]] A_Sel[i][0] [i] A_Sel i 0 A_Sel[i][1] [i] A_Sel i 1 ans_Sel.append(''.join(map(str, s))) ans_Sel.append ans_Sel append ''.join(map(str, s)) ''.join '' join map(str, s) map str s print(' '.join(map(str, ans_Sel))) print ' '.join(map(str, ans_Sel)) ' '.join ' ' join map(str, ans_Sel) map str ans_Sel print(Check(A_Sel, N)) print Check(A_Sel, N) Check A_Sel N def MI(): return map(int, sys.stdin.readline().rstrip().split()) def MI(): return map(int, sys.stdin.readline().rstrip().split()) MI def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a SelectionSort b = [A_Bub[i][0], A_Bub[i][1]] [A_Bub[i][0], A_Bub[i][1]] b def LS(): return list(sys.stdin.readline().rstrip().split()) def LS(): return list(sys.stdin.readline().rstrip().split()) LS A = LS() LS() A A_Bub = [] [] A_Bub def BubleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) def BubleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N) BubleSort s = [A_Sel[i][0], A_Sel[i][1]] [A_Sel[i][0], A_Sel[i][1]] s def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) def LI(): return list(map(int, sys.stdin.readline().rstrip().split())) LI def I(): return int(sys.stdin.readline().rstrip()) def I(): return int(sys.stdin.readline().rstrip()) I def Check(C, N):
    for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
    return 'Stable' def Check(C, N):
    for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
    return 'Stable' Check N = I() I() N ans_Bub = [] [] ans_Bub def S(): return sys.stdin.readline().rstrip() def S(): return sys.stdin.readline().rstrip() S ans_Sel = [] [] ans_Sel A_Sel = [] [] A_Sel
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LS()
A_Bub = []
A_Sel = []
for i in range(N):
    A_Bub.append([A[i][0], int(A[i][1]), i])
    A_Sel.append([A[i][0], int(A[i][1]), i])

#BubleSort(C, N)
def BubleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if C[j][1] < C[j - 1][1]:
                a = C[j]
                b = C[j - 1]
                C[j] = b
                C[j - 1] = a

#SelectionSort(C, N)
def SelectionSort(C, N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        a = C[i]
        b = C[minj]
        C[i] = b
        C[minj] = a

def Check(C, N):
    for i in range(1, N):
        if C[i][1] == C[i - 1][1]:
            if C[i][2] < C[i - 1][2]:
                return 'Not stable'
    return 'Stable'
BubleSort(A_Bub, N)
ans_Bub = []
for i in range(N):
    b = [A_Bub[i][0], A_Bub[i][1]]
    ans_Bub.append(''.join(map(str, b)))
print(' '.join(map(str, ans_Bub)))
print(Check(A_Bub, N))


SelectionSort(A_Sel, N)
ans_Sel = []
for i in range(N):
    s = [A_Sel[i][0], A_Sel[i][1]]
    ans_Sel.append(''.join(map(str, s)))
print(' '.join(map(str, ans_Sel)))
print(Check(A_Sel, N))
