from copy import deepcopy

N = int(input())
oary = input().split()


def bubble_sort(ary):
    for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j]


def selection_sort(ary):

    for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i]


def naive_check(oary, nary):
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

    return True


def is_stable(oary, nary):

    if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable')


bary = deepcopy(oary)
bubble_sort(bary)
print(' '.join(bary))
print('Stable')
# is_stable(oary, bary)

sary = deepcopy(oary)
selection_sort(sary)
print(' '.join(sary))
if bary == sary:
    print('Stable')
else:
    print('Not stable')
# is_stable(oary, sary)
 from copy import deepcopy N = int(input()) N int(input()) int input() input oary = input().split() oary input().split() ().split () input split def bubble_sort(ary):
    for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] bubble_sort for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] i range(N) range N for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] j range(N - 1, i, -1) range N - 1 N 1 i -1 if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] int(ary[j][1:]) < int(ary[j - 1][1:]) int(ary[j][1:]) int ary[j][1:] [j] ary j 1: 1 int(ary[j - 1][1:]) int ary[j - 1][1:] [j - 1] ary j - 1 j 1 1: 1 ary[j], ary[j - 1] = ary[j - 1], ary[j] ary[j] ary j ary[j - 1] ary j - 1 j 1 ary[j - 1] ary j - 1 j 1 ary[j] ary j ary ary def selection_sort(ary):

    for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i] selection_sort for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i] i range(0, N) range 0 N minj = i minj i for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

         j range(i, N) range i N if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

         int(ary[j][1:]) < int(ary[minj][1:]) int(ary[j][1:]) int ary[j][1:] [j] ary j 1: 1 int(ary[minj][1:]) int ary[minj][1:] [minj] ary minj 1: 1 minj = j minj j ary[i], ary[minj] = ary[minj], ary[i] ary[i] ary i ary[minj] ary minj ary[minj] ary minj ary[i] ary i ary ary def naive_check(oary, nary):
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

    return True naive_check for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     i range(1, N - 1) range 1 N - 1 N 1 for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     j range(i + 1, N) range i + 1 i 1 N for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     a range(1, N - 1) range 1 N - 1 N 1 for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     b range(a + 1, N) range a + 1 a 1 N if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     int(oary[i][1:]) == int(nary[j][1:]) int(oary[i][1:]) int oary[i][1:] [i] oary i 1: 1 int(nary[j][1:]) int nary[j][1:] [j] nary j 1: 1 if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

     oary[i] == nary[b] oary[i] oary i nary[b] nary b if oary[j] == nary[a]:
                                return False

     oary[j] == nary[a] oary[j] oary j nary[a] nary a return False False return True True oary oary nary nary def is_stable(oary, nary):

    if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable') is_stable if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable') naive_check(oary, nary) naive_check oary nary print('Stable') print 'Stable' print('Not stable') print 'Not stable' oary oary nary nary bary = deepcopy(oary) bary deepcopy(oary) deepcopy oary bubble_sort(bary) bubble_sort bary print(' '.join(bary)) print ' '.join(bary) ' '.join ' ' join bary print('Stable') print 'Stable' sary = deepcopy(oary) sary deepcopy(oary) deepcopy oary selection_sort(sary) selection_sort sary print(' '.join(sary)) print ' '.join(sary) ' '.join ' ' join sary if bary == sary:
    print('Stable')
else:
    print('Not stable')
# is_stable(oary, sary) bary == sary bary sary print('Stable') print 'Stable' print('Not stable') print 'Not stable' def selection_sort(ary):

    for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i] def selection_sort(ary):

    for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i] selection_sort oary = input().split() input().split() oary def bubble_sort(ary):
    for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] def bubble_sort(ary):
    for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j] bubble_sort N = int(input()) int(input()) N sary = deepcopy(oary) deepcopy(oary) sary def is_stable(oary, nary):

    if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable') def is_stable(oary, nary):

    if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable') is_stable bary = deepcopy(oary) deepcopy(oary) bary def naive_check(oary, nary):
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

    return True def naive_check(oary, nary):
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

    return True naive_check
from copy import deepcopy

N = int(input())
oary = input().split()


def bubble_sort(ary):
    for i in range(N):

        for j in range(N - 1, i, -1):
            if int(ary[j][1:]) < int(ary[j - 1][1:]):
                ary[j], ary[j - 1] = ary[j - 1], ary[j]


def selection_sort(ary):

    for i in range(0, N):
        minj = i

        for j in range(i, N):
            if int(ary[j][1:]) < int(ary[minj][1:]):
                minj = j

        ary[i], ary[minj] = ary[minj], ary[i]


def naive_check(oary, nary):
    for i in range(1, N - 1):
        for j in range(i + 1, N):
            for a in range(1, N - 1):
                for b in range(a + 1, N):

                    if int(oary[i][1:]) == int(nary[j][1:]):
                        if oary[i] == nary[b]:
                            if oary[j] == nary[a]:
                                return False

    return True


def is_stable(oary, nary):

    if naive_check(oary, nary):
        print('Stable')
    else:
        print('Not stable')


bary = deepcopy(oary)
bubble_sort(bary)
print(' '.join(bary))
print('Stable')
# is_stable(oary, bary)

sary = deepcopy(oary)
selection_sort(sary)
print(' '.join(sary))
if bary == sary:
    print('Stable')
else:
    print('Not stable')
# is_stable(oary, sary)

