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


def bubble_sort(cards, num):
    flag = True
    while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

    print(list_to_str(cards))


def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards))


def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable'


def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l])


if __name__ == '__main__':
    num = int(input())
    cards = input().split()
    cards_for_bubble = cards.copy()
    cards_for_selection = cards.copy()

    bubble_sort(cards_for_bubble, num)
    print(is_stable(cards, cards_for_bubble, num))

    selection_sort(cards_for_selection, num)
    print(is_stable(cards, cards_for_selection, num)) def bubble_sort(cards, num):
    flag = True
    while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

    print(list_to_str(cards)) bubble_sort flag = True flag True while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

     flag flag = False flag False for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

     left range(num-1, 0, -1) range num-1 num 1 0 -1 right = left - 1 right left - 1 left 1 if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

     cards[left][1] < cards[right][1] cards[left][1] [left] cards left 1 cards[right][1] [right] cards right 1 cards[right], cards[left] = cards[left], cards[right] cards[right] cards right cards[left] cards left cards[left] cards left cards[right] cards right flag = True flag True print(list_to_str(cards)) print list_to_str(cards) list_to_str cards cards cards num num def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards)) selection_sort for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

     head range(num) range num min_i = head min_i head for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
         target range(head+1, num) range head+1 head 1 num if cards[target][1] < cards[min_i][1]:
                min_i = target
         cards[target][1] < cards[min_i][1] cards[target][1] [target] cards target 1 cards[min_i][1] [min_i] cards min_i 1 min_i = target min_i target cards[head], cards[min_i] = cards[min_i], cards[head] cards[head] cards head cards[min_i] cards min_i cards[min_i] cards min_i cards[head] cards head print(list_to_str(cards)) print list_to_str(cards) list_to_str cards cards cards num num def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable' is_stable for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
     i range(num) range num for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
     j range(i+1, num) range i+1 i 1 num for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
     a range(num) range num for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
     b range(a+1, num) range a+1 a 1 num if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
     before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a] before[i][1] == before[j][1]\
                    and before[i] == after[b] before[i][1] == before[j][1] before[i][1] [i] before i 1 before[j][1] [j] before j 1 before[i] == after[b] before[i] before i after[b] after b before[j] == after[a] before[j] before j after[a] after a return 'Not stable' 'Not stable' return 'Stable' 'Stable' before before after after num num def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l]) list_to_str str(v) for v in l for v in l v l for v in l str(v) str v return delimiter.join([str(v) for v in l]) delimiter.join([str(v) for v in l]) delimiter.join delimiter join [str(v) for v in l] l l delimiter=' ' delimiter ' ' if __name__ == '__main__':
    num = int(input())
    cards = input().split()
    cards_for_bubble = cards.copy()
    cards_for_selection = cards.copy()

    bubble_sort(cards_for_bubble, num)
    print(is_stable(cards, cards_for_bubble, num))

    selection_sort(cards_for_selection, num)
    print(is_stable(cards, cards_for_selection, num)) __name__ == '__main__' __name__ '__main__' num = int(input()) num int(input()) int input() input cards = input().split() cards input().split() ().split () input split cards_for_bubble = cards.copy() cards_for_bubble cards.copy() cards.copy cards copy cards_for_selection = cards.copy() cards_for_selection cards.copy() cards.copy cards copy bubble_sort(cards_for_bubble, num) bubble_sort cards_for_bubble num print(is_stable(cards, cards_for_bubble, num)) print is_stable(cards, cards_for_bubble, num) is_stable cards cards_for_bubble num selection_sort(cards_for_selection, num) selection_sort cards_for_selection num print(is_stable(cards, cards_for_selection, num)) print is_stable(cards, cards_for_selection, num) is_stable cards cards_for_selection num def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable' def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable' is_stable cards = input().split() input().split() cards def bubble_sort(cards, num):
    flag = True
    while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

    print(list_to_str(cards)) def bubble_sort(cards, num):
    flag = True
    while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

    print(list_to_str(cards)) bubble_sort def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards)) def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards)) selection_sort def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l]) def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l]) list_to_str cards_for_selection = cards.copy() cards.copy() cards_for_selection num = int(input()) int(input()) num cards_for_bubble = cards.copy() cards.copy() cards_for_bubble
# -*- coding: utf-8 -*-


def bubble_sort(cards, num):
    flag = True
    while flag:
        flag = False
        for left in range(num-1, 0, -1):
            right = left - 1
            if cards[left][1] < cards[right][1]:
                cards[right], cards[left] = cards[left], cards[right]
                flag = True

    print(list_to_str(cards))


def selection_sort(cards, num):
    for head in range(num):
        min_i = head

        for target in range(head+1, num):
            if cards[target][1] < cards[min_i][1]:
                min_i = target
        cards[head], cards[min_i] = cards[min_i], cards[head]

    print(list_to_str(cards))


def is_stable(before, after, num):
    for i in range(num):
        for j in range(i+1, num):
            for a in range(num):
                for b in range(a+1, num):
                    if before[i][1] == before[j][1]\
                    and before[i] == after[b]\
                    and before[j] == after[a]:
                        return 'Not stable'
    return 'Stable'


def list_to_str(l, delimiter=' '):
    # The default delimiter is one space.
    return delimiter.join([str(v) for v in l])


if __name__ == '__main__':
    num = int(input())
    cards = input().split()
    cards_for_bubble = cards.copy()
    cards_for_selection = cards.copy()

    bubble_sort(cards_for_bubble, num)
    print(is_stable(cards, cards_for_bubble, num))

    selection_sort(cards_for_selection, num)
    print(is_stable(cards, cards_for_selection, num))