# coding=utf-8

def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line)

def combine_list(A_suit, A_rank, N):
    A_after = []
    for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
    return A_after


def bubble_sort(A, index_list, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
    return A, index_list

def selection_sort(A, index_list, N):
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
    return A, index_list

def main():
    N = int(input())
    A = list(input().split())
    A_suit = []
    A_rank = []
    index_list = [x for x in range(N)]
    A_after_bubble = []
    A_after_selection = []

    for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
    A_suit_after_bubble = [0 for x in range(N)]
    A_suit_after_selection = [0 for x in range(N)]

    A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N)
    A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N)

    for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

    for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

    A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N)
    A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N)

    print_list(A_after_bubble)
    print("Stable")
    print_list(A_after_selection)
    if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable")



if __name__ == '__main__':
    main() def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line) print_list first_flag = True first_flag True for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
     val X if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
     first_flag print_line = str(val) print_line str(val) str val first_flag = False first_flag False print_line = print_line + ' ' + str(val) print_line print_line + ' ' + str(val) print_line + ' ' print_line ' ' str(val) str val print(print_line) print print_line X X def combine_list(A_suit, A_rank, N):
    A_after = []
    for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
    return A_after combine_list A_after = [] A_after [] for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
     i range(N) range N A_after.append(A_suit[i] + str(A_rank[i])) A_after.append A_after append A_suit[i] + str(A_rank[i]) A_suit[i] A_suit i str(A_rank[i]) str A_rank[i] A_rank i return A_after A_after A_suit A_suit A_rank A_rank N N def bubble_sort(A, index_list, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
    return A, index_list bubble_sort flag = True flag True i = 0 i 0 while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
     flag flag = False flag False for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
         j range(N-1, i, -1) range N-1 N 1 i -1 if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
         A[j] < A[j-1] A[j] A j A[j-1] A j-1 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 index_list[j-1], index_list[j] = index_list[j], index_list[j-1] index_list[j-1] index_list j-1 j 1 index_list[j] index_list j index_list[j] index_list j index_list[j-1] index_list j-1 j 1 flag = True flag True i += 1 i 1 return A, index_list return A, index_list A index_list A A index_list index_list N N def selection_sort(A, index_list, N):
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
    return A, index_list selection_sort for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
     i range(N) range N min_j = i min_j i for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
         j range(i, N) range i N if A[j] < A[min_j]:
                min_j = j
         A[j] < A[min_j] A[j] A j A[min_j] A min_j min_j = j min_j j if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
     i != min_j i min_j A[i], A[min_j] = A[min_j], A[i] A[i] A i A[min_j] A min_j A[min_j] A min_j A[i] A i index_list[i], index_list[min_j] = index_list[min_j], index_list[i] index_list[i] index_list i index_list[min_j] index_list min_j index_list[min_j] index_list min_j index_list[i] index_list i return A, index_list return A, index_list A index_list A A index_list index_list N N def main():
    N = int(input())
    A = list(input().split())
    A_suit = []
    A_rank = []
    index_list = [x for x in range(N)]
    A_after_bubble = []
    A_after_selection = []

    for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
    A_suit_after_bubble = [0 for x in range(N)]
    A_suit_after_selection = [0 for x in range(N)]

    A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N)
    A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N)

    for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

    for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

    A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N)
    A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N)

    print_list(A_after_bubble)
    print("Stable")
    print_list(A_after_selection)
    if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable") main N = int(input()) N int(input()) int input() input A = list(input().split()) A list(input().split()) list input().split() ().split () input split A_suit = [] A_suit [] A_rank = [] A_rank [] x for x in range(N) for x in range(N) x range(N) range N for x in range(N) x index_list = [x for x in range(N)] index_list [x for x in range(N)] A_after_bubble = [] A_after_bubble [] A_after_selection = [] A_after_selection [] for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
     element A val = list(element) val list(element) list element val_suit = val[0] val_suit val[0] val 0 val_rank = int(val[1]) val_rank int(val[1]) int val[1] val 1 A_suit.append(val_suit) A_suit.append A_suit append val_suit A_rank.append(val_rank) A_rank.append A_rank append val_rank 0 for x in range(N) for x in range(N) x range(N) range N for x in range(N) 0 A_suit_after_bubble = [0 for x in range(N)] A_suit_after_bubble [0 for x in range(N)] 0 for x in range(N) for x in range(N) x range(N) range N for x in range(N) 0 A_suit_after_selection = [0 for x in range(N)] A_suit_after_selection [0 for x in range(N)] A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N) A_rank_after_bubble bubble_sort(A_rank.copy(), index_list.copy(), N) bubble_sort A_rank.copy() A_rank.copy A_rank copy index_list.copy() index_list.copy index_list copy N index_list_after_bubble A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N) A_rank_after_selection selection_sort(A_rank.copy(), index_list.copy(), N) selection_sort A_rank.copy() A_rank.copy A_rank copy index_list.copy() index_list.copy index_list copy N index_list_after_selection for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

     index_before index_after enumerate(index_list_after_bubble) enumerate index_list_after_bubble A_suit_after_bubble[index_before] = A_suit[index_after] A_suit_after_bubble[index_before] A_suit_after_bubble index_before A_suit[index_after] A_suit index_after for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

     index_before index_after enumerate(index_list_after_selection) enumerate index_list_after_selection A_suit_after_selection[index_before] = A_suit[index_after] A_suit_after_selection[index_before] A_suit_after_selection index_before A_suit[index_after] A_suit index_after A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N) A_after_bubble combine_list(A_suit_after_bubble, A_rank_after_bubble, N) combine_list A_suit_after_bubble A_rank_after_bubble N A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N) A_after_selection combine_list(A_suit_after_selection, A_rank_after_selection, N) combine_list A_suit_after_selection A_rank_after_selection N print_list(A_after_bubble) print_list A_after_bubble print("Stable") print "Stable" print_list(A_after_selection) print_list A_after_selection if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable") A_after_selection == A_after_bubble A_after_selection A_after_bubble print("Stable") print "Stable" print("Not stable") print "Not stable" if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def bubble_sort(A, index_list, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
    return A, index_list def bubble_sort(A, index_list, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
    return A, index_list bubble_sort def selection_sort(A, index_list, N):
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
    return A, index_list def selection_sort(A, index_list, N):
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
    return A, index_list selection_sort def main():
    N = int(input())
    A = list(input().split())
    A_suit = []
    A_rank = []
    index_list = [x for x in range(N)]
    A_after_bubble = []
    A_after_selection = []

    for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
    A_suit_after_bubble = [0 for x in range(N)]
    A_suit_after_selection = [0 for x in range(N)]

    A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N)
    A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N)

    for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

    for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

    A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N)
    A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N)

    print_list(A_after_bubble)
    print("Stable")
    print_list(A_after_selection)
    if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable") def main():
    N = int(input())
    A = list(input().split())
    A_suit = []
    A_rank = []
    index_list = [x for x in range(N)]
    A_after_bubble = []
    A_after_selection = []

    for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
    A_suit_after_bubble = [0 for x in range(N)]
    A_suit_after_selection = [0 for x in range(N)]

    A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N)
    A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N)

    for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

    for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

    A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N)
    A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N)

    print_list(A_after_bubble)
    print("Stable")
    print_list(A_after_selection)
    if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable") main def combine_list(A_suit, A_rank, N):
    A_after = []
    for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
    return A_after def combine_list(A_suit, A_rank, N):
    A_after = []
    for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
    return A_after combine_list def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line) def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line) print_list
# coding=utf-8

def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line)

def combine_list(A_suit, A_rank, N):
    A_after = []
    for i in range(N):
        A_after.append(A_suit[i] + str(A_rank[i]))
    return A_after


def bubble_sort(A, index_list, N):
    #print_list(A)
    #flag: ???????????????????????????True???????´?
    flag = True
    i = 0
    while flag:
        flag = False
        for j in range(N-1, i, -1):
            if A[j] < A[j-1]:
                A[j-1], A[j] = A[j], A[j-1]
                index_list[j-1], index_list[j] = index_list[j], index_list[j-1]
                flag = True
        i += 1
    return A, index_list

def selection_sort(A, index_list, N):
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            index_list[i], index_list[min_j] = index_list[min_j], index_list[i]
    return A, index_list

def main():
    N = int(input())
    A = list(input().split())
    A_suit = []
    A_rank = []
    index_list = [x for x in range(N)]
    A_after_bubble = []
    A_after_selection = []

    for element in A:
        val = list(element)
        val_suit = val[0]
        val_rank = int(val[1])
        A_suit.append(val_suit)
        A_rank.append(val_rank)
    A_suit_after_bubble = [0 for x in range(N)]
    A_suit_after_selection = [0 for x in range(N)]

    A_rank_after_bubble, index_list_after_bubble = bubble_sort(A_rank.copy(), index_list.copy(), N)
    A_rank_after_selection, index_list_after_selection = selection_sort(A_rank.copy(), index_list.copy(), N)

    for index_before, index_after in enumerate(index_list_after_bubble):
        A_suit_after_bubble[index_before] = A_suit[index_after]

    for index_before, index_after in enumerate(index_list_after_selection):
        A_suit_after_selection[index_before] = A_suit[index_after]

    A_after_bubble = combine_list(A_suit_after_bubble, A_rank_after_bubble, N)
    A_after_selection = combine_list(A_suit_after_selection, A_rank_after_selection, N)

    print_list(A_after_bubble)
    print("Stable")
    print_list(A_after_selection)
    if A_after_selection == A_after_bubble:
        print("Stable")
    else:
        print("Not stable")



if __name__ == '__main__':
    main()