from copy import deepcopy
def bubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j]

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
        C[i], C[minj] = C[minj], C[i]

N = int(input())
C = list(map(str, input().split()))
C1 = deepcopy(C)
C2 = deepcopy(C)
bubbleSort(C1, N)
selectionSort(C2, N)
O = [[] for _ in range(9)]
for i in range(N):
    j = int(C[i][1]) - 1
    O[j].append(C[i][0])
O1 = [[] for _ in range(9)]
for i in range(N):
    j = int(C1[i][1]) - 1
    O1[j].append(C1[i][0])
O2 = [[] for _ in range(9)]
for i in range(N):
    j = int(C2[i][1]) - 1
    O2[j].append(C2[i][0])
for i in range(N):
    if i == N - 1:
        print(C1[i])
    else:
        print(C1[i], end=' ')
if O == O1:
    print('Stable')
else:
    print('Not stable')
for i in range(N):
    if i == N - 1:
        print(C2[i])
    else:
        print(C2[i], end=' ')
if O == O2:
    print('Stable')
else:
    print('Not stable') from copy import deepcopy def bubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j] bubbleSort for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                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][1] < C[j - 1][1]:
                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][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j] 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 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 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
        C[i], C[minj] = C[minj], C[i] selectionSort for i in range(N):
        minj = i
        for j in range(i, N):
            if C[j][1] < C[minj][1]:
                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][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 C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i C C N N N = int(input()) N int(input()) int input() input C = list(map(str, input().split())) C list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split C1 = deepcopy(C) C1 deepcopy(C) deepcopy C C2 = deepcopy(C) C2 deepcopy(C) deepcopy C bubbleSort(C1, N) bubbleSort C1 N selectionSort(C2, N) selectionSort C2 N [] for _ in range(9) for _ in range(9) _ range(9) range 9 for _ in range(9) [] O = [[] for _ in range(9)] O [[] for _ in range(9)] for i in range(N):
    j = int(C[i][1]) - 1
    O[j].append(C[i][0]) i range(N) range N j = int(C[i][1]) - 1 j int(C[i][1]) - 1 int(C[i][1]) int C[i][1] [i] C i 1 1 O[j].append(C[i][0]) [j].append [j] O j append C[i][0] [i] C i 0 [] for _ in range(9) for _ in range(9) _ range(9) range 9 for _ in range(9) [] O1 = [[] for _ in range(9)] O1 [[] for _ in range(9)] for i in range(N):
    j = int(C1[i][1]) - 1
    O1[j].append(C1[i][0]) i range(N) range N j = int(C1[i][1]) - 1 j int(C1[i][1]) - 1 int(C1[i][1]) int C1[i][1] [i] C1 i 1 1 O1[j].append(C1[i][0]) [j].append [j] O1 j append C1[i][0] [i] C1 i 0 [] for _ in range(9) for _ in range(9) _ range(9) range 9 for _ in range(9) [] O2 = [[] for _ in range(9)] O2 [[] for _ in range(9)] for i in range(N):
    j = int(C2[i][1]) - 1
    O2[j].append(C2[i][0]) i range(N) range N j = int(C2[i][1]) - 1 j int(C2[i][1]) - 1 int(C2[i][1]) int C2[i][1] [i] C2 i 1 1 O2[j].append(C2[i][0]) [j].append [j] O2 j append C2[i][0] [i] C2 i 0 for i in range(N):
    if i == N - 1:
        print(C1[i])
    else:
        print(C1[i], end=' ') i range(N) range N if i == N - 1:
        print(C1[i])
    else:
        print(C1[i], end=' ') i == N - 1 i N - 1 N 1 print(C1[i]) print C1[i] C1 i print(C1[i], end=' ') print C1[i] C1 i end=' ' end ' ' if O == O1:
    print('Stable')
else:
    print('Not stable') O == O1 O O1 print('Stable') print 'Stable' print('Not stable') print 'Not stable' for i in range(N):
    if i == N - 1:
        print(C2[i])
    else:
        print(C2[i], end=' ') i range(N) range N if i == N - 1:
        print(C2[i])
    else:
        print(C2[i], end=' ') i == N - 1 i N - 1 N 1 print(C2[i]) print C2[i] C2 i print(C2[i], end=' ') print C2[i] C2 i end=' ' end ' ' if O == O2:
    print('Stable')
else:
    print('Not stable') O == O2 O O2 print('Stable') print 'Stable' print('Not stable') print 'Not stable' C1 = deepcopy(C) deepcopy(C) C1 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
        C[i], C[minj] = C[minj], C[i] 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
        C[i], C[minj] = C[minj], C[i] selectionSort C = list(map(str, input().split())) list(map(str, input().split())) C O1 = [[] for _ in range(9)] [[] for _ in range(9)] O1 def bubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j] def bubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j] bubbleSort O = [[] for _ in range(9)] [[] for _ in range(9)] O j = int(C[i][1]) - 1 int(C[i][1]) - 1 j O2 = [[] for _ in range(9)] [[] for _ in range(9)] O2 j = int(C1[i][1]) - 1 int(C1[i][1]) - 1 j j = int(C2[i][1]) - 1 int(C2[i][1]) - 1 j N = int(input()) int(input()) N C2 = deepcopy(C) deepcopy(C) C2
from copy import deepcopy
def bubbleSort(C, N):
    for i in range(N):
        for j in range(N - 1, i, - 1):
            if C[j][1] < C[j - 1][1]:
                C[j], C[j - 1] = C[j - 1], C[j]

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
        C[i], C[minj] = C[minj], C[i]

N = int(input())
C = list(map(str, input().split()))
C1 = deepcopy(C)
C2 = deepcopy(C)
bubbleSort(C1, N)
selectionSort(C2, N)
O = [[] for _ in range(9)]
for i in range(N):
    j = int(C[i][1]) - 1
    O[j].append(C[i][0])
O1 = [[] for _ in range(9)]
for i in range(N):
    j = int(C1[i][1]) - 1
    O1[j].append(C1[i][0])
O2 = [[] for _ in range(9)]
for i in range(N):
    j = int(C2[i][1]) - 1
    O2[j].append(C2[i][0])
for i in range(N):
    if i == N - 1:
        print(C1[i])
    else:
        print(C1[i], end=' ')
if O == O1:
    print('Stable')
else:
    print('Not stable')
for i in range(N):
    if i == N - 1:
        print(C2[i])
    else:
        print(C2[i], end=' ')
if O == O2:
    print('Stable')
else:
    print('Not stable')
