def is_stable(A, B, n):
    for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
    return True


def show(Card, n):
    for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value']))


def bubble_sort(A, n):
    Card = A.copy()
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
    show(Card, n)
    return Card


def selection_sort(A, n):
    Card = A.copy()
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
    show(Card, n)
    return Card


def main():
    n = int(input())
    A = list(input().split())
    Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ]
    Card_bubble = bubble_sort(Card, n)
    if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
    Card_selection = selection_sort(Card, n)
    if is_stable(Card, Card_selection, n):
        print('Stable')
    else:
        print('Not stable')


if __name__ == "__main__":
    main()
 def is_stable(A, B, n):
    for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
    return True is_stable for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
     i range(n) range n for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
     j range(i+1, n) range i+1 i 1 n if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
     A[i]['value'] == A[j]['value'] A[i]['value'] [i] A i 'value' A[j]['value'] [j] A j 'value' for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
     a range(n) range n for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
     b range(a+1, n) range a+1 a 1 n if A[i] == B[b] and A[j] == B[a]:
                            return False
     A[i] == B[b] and A[j] == B[a] A[i] == B[b] A[i] A i B[b] B b A[j] == B[a] A[j] A j B[a] B a return False False return True True A A B B n n def show(Card, n):
    for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value'])) show for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value'])) i range(n) range n if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value'])) i != n-1 i n-1 n 1 print(Card[i]['suit'] + str(Card[i]['value']), end=' ') print Card[i]['suit'] + str(Card[i]['value']) Card[i]['suit'] [i] Card i 'suit' str(Card[i]['value']) str Card[i]['value'] [i] Card i 'value' end=' ' end ' ' print(Card[i]['suit'] + str(Card[i]['value'])) print Card[i]['suit'] + str(Card[i]['value']) Card[i]['suit'] [i] Card i 'suit' str(Card[i]['value']) str Card[i]['value'] [i] Card i 'value' Card Card n n def bubble_sort(A, n):
    Card = A.copy()
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
    show(Card, n)
    return Card bubble_sort Card = A.copy() Card A.copy() A.copy A copy flag = 1 flag 1 while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
     flag flag = 0 flag 0 for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
     i range(n-1) range n-1 n 1 if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
     Card[i]['value'] > Card[i+1]['value'] Card[i]['value'] [i] Card i 'value' Card[i+1]['value'] [i+1] Card i+1 i 1 'value' Card[i], Card[i+1] = Card[i+1], Card[i] Card[i] Card i Card[i+1] Card i+1 i 1 Card[i+1] Card i+1 i 1 Card[i] Card i flag = 1 flag 1 show(Card, n) show Card n return Card Card A A n n def selection_sort(A, n):
    Card = A.copy()
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
    show(Card, n)
    return Card selection_sort Card = A.copy() Card A.copy() A.copy A copy for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
     i range(n-1) range n-1 n 1 min_j = i min_j i for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
         j range(i+1, n) range i+1 i 1 n if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
         Card[min_j]['value'] > Card[j]['value'] Card[min_j]['value'] [min_j] Card min_j 'value' Card[j]['value'] [j] Card j 'value' min_j = j min_j j if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
     min_j != i min_j i Card[i], Card[min_j] = Card[min_j], Card[i] Card[i] Card i Card[min_j] Card min_j Card[min_j] Card min_j Card[i] Card i show(Card, n) show Card n return Card Card A A n n def main():
    n = int(input())
    A = list(input().split())
    Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ]
    Card_bubble = bubble_sort(Card, n)
    if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
    Card_selection = selection_sort(Card, n)
    if is_stable(Card, Card_selection, n):
        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 {'value': int(a[1]), 'suit': a[0]}
        for a in A for a in A a A for a in A {'value': int(a[1]), 'suit': a[0]} 'value' 'value' int(a[1]) int a[1] a 1 'suit' 'suit' a[0] a 0 Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ] Card [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ] Card_bubble = bubble_sort(Card, n) Card_bubble bubble_sort(Card, n) bubble_sort Card n if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
     is_stable(Card, Card_bubble, n) is_stable Card Card_bubble n print('Stable') print 'Stable' print('Not stable') print 'Not stable' Card_selection = selection_sort(Card, n) Card_selection selection_sort(Card, n) selection_sort Card n if is_stable(Card, Card_selection, n):
        print('Stable')
    else:
        print('Not stable') is_stable(Card, Card_selection, n) is_stable Card Card_selection n print('Stable') print 'Stable' print('Not stable') print 'Not stable' if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def is_stable(A, B, n):
    for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
    return True def is_stable(A, B, n):
    for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
    return True is_stable def selection_sort(A, n):
    Card = A.copy()
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
    show(Card, n)
    return Card def selection_sort(A, n):
    Card = A.copy()
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
    show(Card, n)
    return Card selection_sort def show(Card, n):
    for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value'])) def show(Card, n):
    for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value'])) show def bubble_sort(A, n):
    Card = A.copy()
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
    show(Card, n)
    return Card def bubble_sort(A, n):
    Card = A.copy()
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
    show(Card, n)
    return Card bubble_sort def main():
    n = int(input())
    A = list(input().split())
    Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ]
    Card_bubble = bubble_sort(Card, n)
    if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
    Card_selection = selection_sort(Card, n)
    if is_stable(Card, Card_selection, n):
        print('Stable')
    else:
        print('Not stable') def main():
    n = int(input())
    A = list(input().split())
    Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ]
    Card_bubble = bubble_sort(Card, n)
    if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
    Card_selection = selection_sort(Card, n)
    if is_stable(Card, Card_selection, n):
        print('Stable')
    else:
        print('Not stable') main
def is_stable(A, B, n):
    for i in range(n):
        for j in range(i+1, n):
            if A[i]['value'] == A[j]['value']:
                for a in range(n):
                    for b in range(a+1, n):
                        if A[i] == B[b] and A[j] == B[a]:
                            return False
    return True


def show(Card, n):
    for i in range(n):
        if i != n-1:
            print(Card[i]['suit'] + str(Card[i]['value']), end=' ')
        else:
            print(Card[i]['suit'] + str(Card[i]['value']))


def bubble_sort(A, n):
    Card = A.copy()
    flag = 1
    while flag:
        flag = 0
        for i in range(n-1):
            if Card[i]['value'] > Card[i+1]['value']:
                Card[i], Card[i+1] = Card[i+1], Card[i]
                flag = 1
    show(Card, n)
    return Card


def selection_sort(A, n):
    Card = A.copy()
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if Card[min_j]['value'] > Card[j]['value']:
                min_j = j
        if min_j != i:
            Card[i], Card[min_j] = Card[min_j], Card[i]
    show(Card, n)
    return Card


def main():
    n = int(input())
    A = list(input().split())
    Card = [
        {'value': int(a[1]), 'suit': a[0]}
        for a in A
    ]
    Card_bubble = bubble_sort(Card, n)
    if is_stable(Card, Card_bubble, n):
        print('Stable')
    else:
        print('Not stable')
    Card_selection = selection_sort(Card, n)
    if is_stable(Card, Card_selection, n):
        print('Stable')
    else:
        print('Not stable')


if __name__ == "__main__":
    main()

