# -*- coding: utf-8 -*-


def bubbleSort(A, N):

    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num


def selectionSort(A, N):
    count = 0

    for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

    return A, count


if __name__ == '__main__':

    N = int(input())
    A = [(a[0], int(a[1])) for a in input().split(" ")]

    A_bubble, _ = bubbleSort(A.copy(), N)
    A_selection, _ = selectionSort(A.copy(), N)

    A_bubble = [a[0] + str(a[1]) for a in A_bubble]
    A_selection = [a[0] + str(a[1]) for a in A_selection]

    print(' '.join(A_bubble))
    print("Stable")
    print(" ".join(A_selection))
    if A_selection == A_bubble:
        print("Stable")
    else:
        print("Not stable")
 def bubbleSort(A, N):

    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num bubbleSort flag = True flag True i = 0 i 0 inv_num = 0 inv_num 0 while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

     flag flag = False flag False for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
         j range(N - 1, i, -1) range N - 1 N 1 i -1 if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
         A[j][1] < A[j - 1][1] A[j][1] [j] A j 1 A[j - 1][1] [j - 1] A j - 1 j 1 1 A[j], A[j - 1] = A[j - 1], A[j] A[j] A j A[j - 1] A j - 1 j 1 A[j - 1] A j - 1 j 1 A[j] A j flag = True flag True inv_num += 1 inv_num 1 i += 1 i 1 return A, inv_num return A, inv_num A inv_num A A N N def selectionSort(A, N):
    count = 0

    for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

    return A, count selectionSort count = 0 count 0 for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

     i range(N) range N minj = i minj i for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
         j range(i, N) range i N if A[j][1] < A[minj][1]:
                minj = j
         A[j][1] < A[minj][1] A[j][1] [j] A j 1 A[minj][1] [minj] A minj 1 minj = j minj j if minj != i:
            count += 1

         minj != i minj i count += 1 count 1 A[i], A[minj] = A[minj], A[i] A[i] A i A[minj] A minj A[minj] A minj A[i] A i return A, count return A, count A count A A N N if __name__ == '__main__':

    N = int(input())
    A = [(a[0], int(a[1])) for a in input().split(" ")]

    A_bubble, _ = bubbleSort(A.copy(), N)
    A_selection, _ = selectionSort(A.copy(), N)

    A_bubble = [a[0] + str(a[1]) for a in A_bubble]
    A_selection = [a[0] + str(a[1]) for a in A_selection]

    print(' '.join(A_bubble))
    print("Stable")
    print(" ".join(A_selection))
    if A_selection == A_bubble:
        print("Stable")
    else:
        print("Not stable") __name__ == '__main__' __name__ '__main__' N = int(input()) N int(input()) int input() input (a[0], int(a[1])) for a in input().split(" ") for a in input().split(" ") a input().split(" ") ().split () input split " " for a in input().split(" ") (a[0], int(a[1])) a[0] a 0 int(a[1]) int a[1] a 1 A = [(a[0], int(a[1])) for a in input().split(" ")] A [(a[0], int(a[1])) for a in input().split(" ")] A_bubble, _ = bubbleSort(A.copy(), N) A_bubble bubbleSort(A.copy(), N) bubbleSort A.copy() A.copy A copy N _ A_selection, _ = selectionSort(A.copy(), N) A_selection selectionSort(A.copy(), N) selectionSort A.copy() A.copy A copy N _ a[0] + str(a[1]) for a in A_bubble for a in A_bubble a A_bubble for a in A_bubble a[0] + str(a[1]) a[0] a 0 str(a[1]) str a[1] a 1 A_bubble = [a[0] + str(a[1]) for a in A_bubble] A_bubble [a[0] + str(a[1]) for a in A_bubble] a[0] + str(a[1]) for a in A_selection for a in A_selection a A_selection for a in A_selection a[0] + str(a[1]) a[0] a 0 str(a[1]) str a[1] a 1 A_selection = [a[0] + str(a[1]) for a in A_selection] A_selection [a[0] + str(a[1]) for a in A_selection] print(' '.join(A_bubble)) print ' '.join(A_bubble) ' '.join ' ' join A_bubble print("Stable") print "Stable" print(" ".join(A_selection)) print " ".join(A_selection) " ".join " " join A_selection if A_selection == A_bubble:
        print("Stable")
    else:
        print("Not stable") A_selection == A_bubble A_selection A_bubble print("Stable") print "Stable" print("Not stable") print "Not stable" A_bubble = [a[0] + str(a[1]) for a in A_bubble] [a[0] + str(a[1]) for a in A_bubble] A_bubble A_bubble, _ = bubbleSort(A.copy(), N) bubbleSort(A.copy(), N) A_bubble def selectionSort(A, N):
    count = 0

    for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

    return A, count def selectionSort(A, N):
    count = 0

    for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

    return A, count selectionSort _ = selectionSort(A.copy(), N) selectionSort(A.copy(), N) _ _ = bubbleSort(A.copy(), N) bubbleSort(A.copy(), N) _ def bubbleSort(A, N):

    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num def bubbleSort(A, N):

    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num bubbleSort A = [(a[0], int(a[1])) for a in input().split(" ")] [(a[0], int(a[1])) for a in input().split(" ")] A A_selection = [a[0] + str(a[1]) for a in A_selection] [a[0] + str(a[1]) for a in A_selection] A_selection A_selection, _ = selectionSort(A.copy(), N) selectionSort(A.copy(), N) A_selection N = int(input()) int(input()) N
# -*- coding: utf-8 -*-


def bubbleSort(A, N):

    flag = True  # 未ソートの可能性があるか
    i = 0  # 未ソート部分の先頭
    inv_num = 0  # 転倒数
    while flag:
        flag = False
        for j in range(N - 1, i, -1):
            if A[j][1] < A[j - 1][1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                flag = True
                inv_num += 1
        i += 1

    return A, inv_num


def selectionSort(A, N):
    count = 0

    for i in range(N):

        minj = i
        for j in range(i, N):
            if A[j][1] < A[minj][1]:
                minj = j
        if minj != i:
            count += 1

        A[i], A[minj] = A[minj], A[i]

    return A, count


if __name__ == '__main__':

    N = int(input())
    A = [(a[0], int(a[1])) for a in input().split(" ")]

    A_bubble, _ = bubbleSort(A.copy(), N)
    A_selection, _ = selectionSort(A.copy(), N)

    A_bubble = [a[0] + str(a[1]) for a in A_bubble]
    A_selection = [a[0] + str(a[1]) for a in A_selection]

    print(' '.join(A_bubble))
    print("Stable")
    print(" ".join(A_selection))
    if A_selection == A_bubble:
        print("Stable")
    else:
        print("Not stable")

