
def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable")


def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret)


def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a)


if __name__ == '__main__':
    main() def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable") bubblesort r = a[:] r a[:] a : for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

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

     j range(n-1, i, -1) range n-1 n 1 i -1 if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

     int(r[j][1]) < int(r[j-1][1]) int(r[j][1]) int r[j][1] [j] r j 1 int(r[j-1][1]) int r[j-1][1] [j-1] r j-1 j 1 1 r[j], r[j-1] = r[j-1], r[j] r[j] r j r[j-1] r j-1 j 1 r[j-1] r j-1 j 1 r[j] r j print(*r) print *r r print("Stable") print "Stable" n n a a def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret) selectionsort r = a[:] r a[:] a : ret = "Stable" ret "Stable" for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

     i range(n) range n tmp = i tmp i for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
         j range(i, n) range i n if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
         int(r[j][1]) < int(r[tmp][1]) int(r[j][1]) int r[j][1] [j] r j 1 int(r[tmp][1]) int r[tmp][1] [tmp] r tmp 1 tmp = j tmp j r[i], r[tmp] = r[tmp], r[i] r[i] r i r[tmp] r tmp r[tmp] r tmp r[i] r i for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

     i range(n) range n for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

     j range(i+1, n) range i+1 i 1 n for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

     k range(n) range n for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

     l range(k+1, n) range k+1 k 1 n if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

     int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k] int(a[i][1]) == int(a[j][1]) and a[i] == r[l] int(a[i][1]) == int(a[j][1]) int(a[i][1]) int a[i][1] [i] a i 1 int(a[j][1]) int a[j][1] [j] a j 1 a[i] == r[l] a[i] a i r[l] r l a[j] == r[k] a[j] a j r[k] r k ret = "Not stable" ret "Not stable" print(*r) print *r r print(ret) print ret n n a a def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a) main n = int(input()) n int(input()) int input() input a = list(input().split()) a list(input().split()) list input().split() ().split () input split bubblesort(n, a) bubblesort n a selectionsort(n, a) selectionsort n a if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable") def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable") bubblesort def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a) def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a) main def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret) def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret) selectionsort

def bubblesort(n, a):
    r = a[:]
    for i in range(n):
        for j in range(n-1, i, -1):
            if int(r[j][1]) < int(r[j-1][1]):
                r[j], r[j-1] = r[j-1], r[j]

    print(*r)
    print("Stable")


def selectionsort(n, a):
    r = a[:]
    ret = "Stable"
    for i in range(n):
        tmp = i
        for j in range(i, n):
            if int(r[j][1]) < int(r[tmp][1]):
                tmp = j
        r[i], r[tmp] = r[tmp], r[i]

    for i in range(n):
        for j in range(i+1, n):
            for k in range(n):
                for l in range(k+1, n):
                    if int(a[i][1]) == int(a[j][1]) and a[i] == r[l] and a[j] == r[k]:
                        ret = "Not stable"

    print(*r)
    print(ret)


def main():
    n = int(input())
    a = list(input().split())

    bubblesort(n, a)
    selectionsort(n, a)


if __name__ == '__main__':
    main()
