# coding: utf-8
# Here your code !

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


def SelectionSort(C, N):
    for i in range(N):
        minj = i

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


if __name__ == '__main__':
    n = int(input())
    data = input().split()
    selection = data[:]
    data = BubbleSort(data, n)
#    print(" ".join(BubbleSort(data, n)))
    print(" ".join(data))
    print("Stable")
    selection = SelectionSort(selection, n)
    print(" ".join(selection))
    if data == selection:
        print("Stable")
    else:
        print("Not stable") def BubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(1, N):
            if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
    return A BubbleSort count = 0 count 0 flag = 1 flag 1 while flag:
        flag = 0
        for i in range(1, N):
            if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
     flag flag = 0 flag 0 for i in range(1, N):
            if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
     i range(1, N) range 1 N if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
     int(A[N - i][1]) < int(A[N - i - 1][1]) int(A[N - i][1]) int A[N - i][1] [N - i] A N - i N i 1 int(A[N - i - 1][1]) int A[N - i - 1][1] [N - i - 1] A N - i - 1 N - i N i 1 1 A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i] A[N - i] A N - i N i A[N - i - 1] A N - i - 1 N - i N i 1 A[N - i - 1] A N - i - 1 N - i N i 1 A[N - i] A N - i N i flag = 1 flag 1 return A A A A N N def SelectionSort(C, N):
    for i in range(N):
        minj = i

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

        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
     i range(N) range N 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 i != minj:
            C[i], C[minj] = C[minj], C[i]
     i != minj i minj 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 if __name__ == '__main__':
    n = int(input())
    data = input().split()
    selection = data[:]
    data = BubbleSort(data, n)
#    print(" ".join(BubbleSort(data, n)))
    print(" ".join(data))
    print("Stable")
    selection = SelectionSort(selection, n)
    print(" ".join(selection))
    if data == selection:
        print("Stable")
    else:
        print("Not stable") __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input data = input().split() data input().split() ().split () input split selection = data[:] selection data[:] data : data = BubbleSort(data, n) data BubbleSort(data, n) BubbleSort data n print(" ".join(data)) print " ".join(data) " ".join " " join data print("Stable") print "Stable" selection = SelectionSort(selection, n) selection SelectionSort(selection, n) SelectionSort selection n print(" ".join(selection)) print " ".join(selection) " ".join " " join selection if data == selection:
        print("Stable")
    else:
        print("Not stable") data == selection data selection print("Stable") print "Stable" print("Not stable") print "Not stable" data = BubbleSort(data, n) BubbleSort(data, n) data selection = SelectionSort(selection, n) SelectionSort(selection, n) selection selection = data[:] data[:] selection def SelectionSort(C, N):
    for i in range(N):
        minj = i

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

        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
        if i != minj:
            C[i], C[minj] = C[minj], C[i]
    return C SelectionSort def BubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(1, N):
            if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
    return A def BubbleSort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(1, N):
            if int(A[N - i][1]) < int(A[N - i - 1][1]):
                A[N - i], A[N - i - 1] = A[N - i - 1], A[N - i]
                flag = 1
    return A BubbleSort n = int(input()) int(input()) n data = input().split() input().split() data
# coding: utf-8
# Here your code !

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


def SelectionSort(C, N):
    for i in range(N):
        minj = i

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


if __name__ == '__main__':
    n = int(input())
    data = input().split()
    selection = data[:]
    data = BubbleSort(data, n)
#    print(" ".join(BubbleSort(data, n)))
    print(" ".join(data))
    print("Stable")
    selection = SelectionSort(selection, n)
    print(" ".join(selection))
    if data == selection:
        print("Stable")
    else:
        print("Not stable")