import re


def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C


def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C


def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True


if __name__ == '__main__':
    N = int(input())
    # ??????????????????????????????????????¨?????¨??°?????¨?????????????????????????´?
    cards = input().split()
    C = [[] for i in range(N)]
    for i in range(N):
        # ??????????¨??????¨??°????????????
        symbol_and_num = re.findall(r'(\d+|\D+)', cards[i])
        C[i] = [symbol_and_num[0], int(symbol_and_num[1])]

    # ??????????????????????????????
    bubble_sorted = bubble_sort(C.copy(), N)
    # ?¨??????¨???????????????
    bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted]
    # ?????????????????????
    print(' '.join(bubble_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, bubble_sorted) else 'Not stable')

    # ?????????????????????
    selection_sorted = selection_sort(C.copy(), N)
    # ?¨??????¨???????????????
    selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted]
    # ?????????????????????
    print(' '.join(selection_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, selection_sorted) else 'Not stable') import re re def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C bubble_sort for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
     i range(N) range N for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
     j reversed(range(1, N)) reversed range(1, N) range 1 N if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
     C[j][1] < C[j-1][1] C[j][1] [j] C j 1 C[j-1][1] [j-1] C j-1 j 1 1 C[j], C[j-1] = C[j-1], C[j] C[j] C j C[j-1] C j-1 j 1 C[j-1] C j-1 j 1 C[j] C j return C C C C N N def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C selection_sort for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
     i range(N) range N minj = i minj i for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
         j range(i, N) range i N if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
         C[j][1] < C[minj][1] C[j][1] [j] C j 1 C[minj][1] [minj] C minj 1 minj = j minj j 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 def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True is_stable c_container = [] c_container [] s_container = [] s_container [] for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

     i range(len(C)) range len(C) len C for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
         j range(len(C)) range len(C) len C if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
         C[i][1] == C[j][1] C[i][1] [i] C i 1 C[j][1] [j] C j 1 c_container.append(C[j][0]) c_container.append c_container append C[j][0] [j] C j 0 if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
         len(c_container) >= 2 len(c_container) len c_container 2 for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
             k range(len(sorted)) range len(sorted) len sorted if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
             sorted[k][1] == C[i][1] sorted[k][1] [k] sorted k 1 C[i][1] [i] C i 1 s_container.append(sorted[k][0]) s_container.append s_container append sorted[k][0] [k] sorted k 0 if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
         c_container != s_container c_container s_container return False False c_container = [] c_container [] s_container = [] s_container [] return True True C C sorted sorted if __name__ == '__main__':
    N = int(input())
    # ??????????????????????????????????????¨?????¨??°?????¨?????????????????????????´?
    cards = input().split()
    C = [[] for i in range(N)]
    for i in range(N):
        # ??????????¨??????¨??°????????????
        symbol_and_num = re.findall(r'(\d+|\D+)', cards[i])
        C[i] = [symbol_and_num[0], int(symbol_and_num[1])]

    # ??????????????????????????????
    bubble_sorted = bubble_sort(C.copy(), N)
    # ?¨??????¨???????????????
    bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted]
    # ?????????????????????
    print(' '.join(bubble_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, bubble_sorted) else 'Not stable')

    # ?????????????????????
    selection_sorted = selection_sort(C.copy(), N)
    # ?¨??????¨???????????????
    selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted]
    # ?????????????????????
    print(' '.join(selection_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, selection_sorted) else 'Not stable') __name__ == '__main__' __name__ '__main__' N = int(input()) N int(input()) int input() input cards = input().split() cards input().split() ().split () input split [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] C = [[] for i in range(N)] C [[] for i in range(N)] for i in range(N):
        # ??????????¨??????¨??°????????????
        symbol_and_num = re.findall(r'(\d+|\D+)', cards[i])
        C[i] = [symbol_and_num[0], int(symbol_and_num[1])]

    # ??????????????????????????????
     i range(N) range N symbol_and_num = re.findall(r'(\d+|\D+)', cards[i]) symbol_and_num re.findall(r'(\d+|\D+)', cards[i]) re.findall re findall r'(\d+|\D+)' cards[i] cards i C[i] = [symbol_and_num[0], int(symbol_and_num[1])] C[i] C i [symbol_and_num[0], int(symbol_and_num[1])] symbol_and_num[0] symbol_and_num 0 int(symbol_and_num[1]) int symbol_and_num[1] symbol_and_num 1 bubble_sorted = bubble_sort(C.copy(), N) bubble_sorted bubble_sort(C.copy(), N) bubble_sort C.copy() C.copy C copy N val[0] + str(val[1]) for val in bubble_sorted for val in bubble_sorted val bubble_sorted for val in bubble_sorted val[0] + str(val[1]) val[0] val 0 str(val[1]) str val[1] val 1 bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted] bubble_sorted_cards [val[0] + str(val[1]) for val in bubble_sorted] print(' '.join(bubble_sorted_cards)) print ' '.join(bubble_sorted_cards) ' '.join ' ' join bubble_sorted_cards print('Stable' if is_stable(C, bubble_sorted) else 'Not stable') print 'Stable' if is_stable(C, bubble_sorted) else 'Not stable' is_stable(C, bubble_sorted) is_stable C bubble_sorted 'Stable' 'Not stable' selection_sorted = selection_sort(C.copy(), N) selection_sorted selection_sort(C.copy(), N) selection_sort C.copy() C.copy C copy N val[0] + str(val[1]) for val in selection_sorted for val in selection_sorted val selection_sorted for val in selection_sorted val[0] + str(val[1]) val[0] val 0 str(val[1]) str val[1] val 1 selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted] selection_sorted_cards [val[0] + str(val[1]) for val in selection_sorted] print(' '.join(selection_sorted_cards)) print ' '.join(selection_sorted_cards) ' '.join ' ' join selection_sorted_cards print('Stable' if is_stable(C, selection_sorted) else 'Not stable') print 'Stable' if is_stable(C, selection_sorted) else 'Not stable' is_stable(C, selection_sorted) is_stable C selection_sorted 'Stable' 'Not stable' symbol_and_num = re.findall(r'(\d+|\D+)', cards[i]) re.findall(r'(\d+|\D+)', cards[i]) symbol_and_num bubble_sorted = bubble_sort(C.copy(), N) bubble_sort(C.copy(), N) bubble_sorted C = [[] for i in range(N)] [[] for i in range(N)] C def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C selection_sort bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted] [val[0] + str(val[1]) for val in bubble_sorted] bubble_sorted_cards selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted] [val[0] + str(val[1]) for val in selection_sorted] selection_sorted_cards cards = input().split() input().split() cards def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C bubble_sort def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True is_stable selection_sorted = selection_sort(C.copy(), N) selection_sort(C.copy(), N) selection_sorted N = int(input()) int(input()) N
import re


def bubble_sort(C, N):
    for i in range(N):
        for j in reversed(range(1, N)):
            if C[j][1] < C[j-1][1]:
                # 1???????????????????°????????????°??????
                C[j], C[j-1] = C[j-1], C[j]
    return C


def selection_sort(C, N):
    for i in range(N):
        # i ?????????????????¨????????????
        minj = i
        for j in range(i, N):
            # ?????????????????¨??????????°???????????????????
            if C[j][1] < C[minj][1]:
                minj = j
        # ??????????????¨??????????°????????????????????????\????????????
        C[i], C[minj] = C[minj], C[i]
    return C


def is_stable(C, sorted):
    # ????????°???????????????????¨???????????????????????´???????????????????
    c_container = []
    s_container = []
    for i in range(len(C)):
        # C ??§ i ????????\?????????????????§????????°????????????????????¢???
        for j in range(len(C)):
            # ????????°???????????????????????£?????´???
            if C[i][1] == C[j][1]:
                # C ????????????????¨?????????????
                c_container.append(C[j][0])
        # ????????°???????????????????????¨????????´???
        if len(c_container) >= 2:
            # C ??§ i ???????????????????????°?????¨????????°????????????????????¢???
            for k in range(len(sorted)):
                # ????±??????????????????°?????¨????????°???????¨?????????????????????????????????????????????????????´?
                if sorted[k][1] == C[i][1]:
                    s_container.append(sorted[k][0])
            # ??????????????´?????????????????£?????´???
            if c_container != s_container:
                # Not Stable
                return False
        # ?????????????????????
        c_container = []
        s_container = []

    return True


if __name__ == '__main__':
    N = int(input())
    # ??????????????????????????????????????¨?????¨??°?????¨?????????????????????????´?
    cards = input().split()
    C = [[] for i in range(N)]
    for i in range(N):
        # ??????????¨??????¨??°????????????
        symbol_and_num = re.findall(r'(\d+|\D+)', cards[i])
        C[i] = [symbol_and_num[0], int(symbol_and_num[1])]

    # ??????????????????????????????
    bubble_sorted = bubble_sort(C.copy(), N)
    # ?¨??????¨???????????????
    bubble_sorted_cards = [val[0] + str(val[1]) for val in bubble_sorted]
    # ?????????????????????
    print(' '.join(bubble_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, bubble_sorted) else 'Not stable')

    # ?????????????????????
    selection_sorted = selection_sort(C.copy(), N)
    # ?¨??????¨???????????????
    selection_sorted_cards = [val[0] + str(val[1]) for val in selection_sorted]
    # ?????????????????????
    print(' '.join(selection_sorted_cards))
    # stable or not stable
    print('Stable' if is_stable(C, selection_sorted) else 'Not stable')