def selectionSort(n, A):
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return A


def bubbleSort(n, A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
    return A


def stableSort(input, output):
    n = len(input)
    for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
    return print("Stable")


if __name__ == '__main__':
    n = int(input())
    R = list(map(str, input().split()))
    C = R[:]
    D = R[:]  #????????¨?????????
    SR = selectionSort(n, R)
    BR = bubbleSort(n, C)
    print(*BR)
    stableSort(D,BR)
    print(*SR)
    stableSort(D,SR) def selectionSort(n, A):
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return A selectionSort cnt = 0 cnt 0 for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
     i range(n) range n minj = i minj i for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
         j range(i, n) range i n if A[minj][1] > A[j][1]:
                minj = j
         A[minj][1] > A[j][1] A[minj][1] [minj] A minj 1 A[j][1] [j] A j 1 minj = j minj j if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
     i != minj i minj A[i], A[minj] = A[minj], A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i cnt += 1 cnt 1 return A A n n A A def bubbleSort(n, A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
    return A bubbleSort flag = True flag True cnt = 0 cnt 0 while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
     flag flag = False flag False for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
     j range(n - 1, 0, -1) range n - 1 n 1 0 -1 if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
     A[j - 1][1] > A[j][1] A[j - 1][1] [j - 1] A j - 1 j 1 1 A[j][1] [j] A j 1 A[j - 1], A[j] = A[j], A[j - 1] A[j - 1] A j - 1 j 1 A[j] A j A[j] A j A[j - 1] A j - 1 j 1 cnt += 1 cnt 1 flag = True flag True return A A n n A A def stableSort(input, output):
    n = len(input)
    for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
    return print("Stable") stableSort n = len(input) n len(input) len input for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
     i range(n) range n for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
     j range(i + 1, n) range i + 1 i 1 n for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
     a range(n) range n for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
     b range(a + 1, n) range a + 1 a 1 n if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
     input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a] input[i][1] == input[j][1] and input[i] == output[b] input[i][1] == input[j][1] input[i][1] [i] input i 1 input[j][1] [j] input j 1 input[i] == output[b] input[i] input i output[b] output b input[j] == output[a] input[j] input j output[a] output a return print("Not stable") print("Not stable") print "Not stable" return print("Stable") print("Stable") print "Stable" input input output output if __name__ == '__main__':
    n = int(input())
    R = list(map(str, input().split()))
    C = R[:]
    D = R[:]  #????????¨?????????
    SR = selectionSort(n, R)
    BR = bubbleSort(n, C)
    print(*BR)
    stableSort(D,BR)
    print(*SR)
    stableSort(D,SR) __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input R = list(map(str, input().split())) R list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split C = R[:] C R[:] R : D = R[:] D R[:] R : SR = selectionSort(n, R) SR selectionSort(n, R) selectionSort n R BR = bubbleSort(n, C) BR bubbleSort(n, C) bubbleSort n C print(*BR) print *BR BR stableSort(D,BR) stableSort D BR print(*SR) print *SR SR stableSort(D,SR) stableSort D SR def bubbleSort(n, A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
    return A def bubbleSort(n, A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
    return A bubbleSort BR = bubbleSort(n, C) bubbleSort(n, C) BR R = list(map(str, input().split())) list(map(str, input().split())) R D = R[:] R[:] D def selectionSort(n, A):
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return A def selectionSort(n, A):
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return A selectionSort n = int(input()) int(input()) n C = R[:] R[:] C def stableSort(input, output):
    n = len(input)
    for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
    return print("Stable") def stableSort(input, output):
    n = len(input)
    for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
    return print("Stable") stableSort SR = selectionSort(n, R) selectionSort(n, R) SR
def selectionSort(n, A):
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[minj][1] > A[j][1]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return A


def bubbleSort(n, A):
    flag = True
    cnt = 0
    while flag:
        flag = False
        for j in range(n - 1, 0, -1):
            if A[j - 1][1] > A[j][1]:
                A[j - 1], A[j] = A[j], A[j - 1]
                cnt += 1
                flag = True
    return A


def stableSort(input, output):
    n = len(input)
    for i in range(n):
        for j in range(i + 1, n):
            for a in range(n):
                for b in range(a + 1, n):
                    if input[i][1] == input[j][1] and input[i] == output[b] and input[j] == output[a]:
                        return print("Not stable")
    return print("Stable")


if __name__ == '__main__':
    n = int(input())
    R = list(map(str, input().split()))
    C = R[:]
    D = R[:]  #????????¨?????????
    SR = selectionSort(n, R)
    BR = bubbleSort(n, C)
    print(*BR)
    stableSort(D,BR)
    print(*SR)
    stableSort(D,SR)