import copy


def BubbleSort(C, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
    return C


def SelectionSort(C, n):
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
    return C


n = int(input())
C = list(input().split())
C2 = C.copy()

print(*BubbleSort(C, n), sep=' ')
print("Stable")
print(*SelectionSort(C2, n), sep=' ')
if C ==C2:
    print("Stable")
else:
    print("Not stable")
 import copy copy def BubbleSort(C, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
    return C BubbleSort flag = 1 flag 1 while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
     flag flag = 0 flag 0 for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
     i range(n-1, 0, -1) range n-1 n 1 0 -1 if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
     int(C[i][1]) < int(C[i - 1][1]) int(C[i][1]) int C[i][1] [i] C i 1 int(C[i - 1][1]) int C[i - 1][1] [i - 1] C i - 1 i 1 1 C[i], C[i-1] = C[i-1], C[i] C[i] C i C[i-1] C i-1 i 1 C[i-1] C i-1 i 1 C[i] C i flag = 1 flag 1 return C C C C n n def SelectionSort(C, n):
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
    return C SelectionSort for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
     i range(n-1) range n-1 n 1 minj = i minj i for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
         j range(i, n) range i n if int(C[j][1]) < int(C[minj][1]):
                minj = j
         int(C[j][1]) < int(C[minj][1]) int(C[j][1]) int C[j][1] [j] C j 1 int(C[minj][1]) int C[minj][1] [minj] C minj 1 minj = j minj j if minj != i:
            C[i], C[minj] = C[minj], C[i]
     minj != i minj i C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i return C C C C n n n = int(input()) n int(input()) int input() input C = list(input().split()) C list(input().split()) list input().split() ().split () input split C2 = C.copy() C2 C.copy() C.copy C copy print(*BubbleSort(C, n), sep=' ') print *BubbleSort(C, n) BubbleSort(C, n) BubbleSort C n sep=' ' sep ' ' print("Stable") print "Stable" print(*SelectionSort(C2, n), sep=' ') print *SelectionSort(C2, n) SelectionSort(C2, n) SelectionSort C2 n sep=' ' sep ' ' if C ==C2:
    print("Stable")
else:
    print("Not stable") C ==C2 C C2 print("Stable") print "Stable" print("Not stable") print "Not stable" def BubbleSort(C, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
    return C def BubbleSort(C, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
    return C BubbleSort n = int(input()) int(input()) n C = list(input().split()) list(input().split()) C C2 = C.copy() C.copy() C2 def SelectionSort(C, n):
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
    return C def SelectionSort(C, n):
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
    return C SelectionSort
import copy


def BubbleSort(C, n):
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i-1] = C[i-1], C[i]
                flag = 1
    return C


def SelectionSort(C, n):
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if minj != i:
            C[i], C[minj] = C[minj], C[i]
    return C


n = int(input())
C = list(input().split())
C2 = C.copy()

print(*BubbleSort(C, n), sep=' ')
print("Stable")
print(*SelectionSort(C2, n), sep=' ')
if C ==C2:
    print("Stable")
else:
    print("Not stable")

