# coding: utf-8

def bubble_sort(A, N):
    C = list(A)
    for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
    return C

def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True

def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c))

def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n)

main() def bubble_sort(A, N):
    C = list(A)
    for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
    return C bubble_sort C = list(A) C list(A) list A for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
     i xrange(N) xrange N for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
     j reversed(xrange(i+1, N)) reversed xrange(i+1, N) xrange i+1 i 1 N if C[j][1] < C[j-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 A A N N def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C selection_sort C = list(A) C list(A) list A for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
     i xrange(N) xrange N minj = i minj i for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
         j xrange(i, N) xrange 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 A A N N def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True is_stable for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
     i xrange(n-1) xrange n-1 n 1 if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
     c[i][1] == c[i+1][1] c[i][1] [i] c i 1 c[i+1][1] [i+1] c i+1 i 1 1 if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
     a.index(c[i]) > a.index(c[i+1]) a.index(c[i]) a.index a index c[i] c i a.index(c[i+1]) a.index a index c[i+1] c i+1 i 1 print return False False print return True True a a c c n n def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c)) print_c print " ".join(map(lambda c:c[0] + str(c[1]), c)) " ".join " " join map(lambda c:c[0] + str(c[1]), c) map lambda c:c[0] + str(c[1]) c[0] + str(c[1]) c[0] c 0 str(c[1]) str c[1] c 1 c c c c def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n) main n = input() n input() input c = map(lambda c:(c[0], int(c[1:])), raw_input().split()) c map(lambda c:(c[0], int(c[1:])), raw_input().split()) map lambda c:(c[0], int(c[1:])) (c[0], int(c[1:])) c[0] c 0 int(c[1:]) int c[1:] c 1: 1 c raw_input().split() ().split () raw_input split bubble = bubble_sort(c, n) bubble bubble_sort(c, n) bubble_sort c n selection = selection_sort(c, n) selection selection_sort(c, n) selection_sort c n print_c(bubble) print_c bubble is_stable(c, bubble, n) is_stable c bubble n print_c(selection) print_c selection is_stable(c, selection, n) is_stable c selection n main() main def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n) def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n) main def bubble_sort(A, N):
    C = list(A)
    for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
    return C def bubble_sort(A, N):
    C = list(A)
    for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
    return C bubble_sort def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C selection_sort def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c)) def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c)) print_c def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True is_stable
# coding: utf-8

def bubble_sort(A, N):
    C = list(A)
    for i in xrange(N):
        for j in reversed(xrange(i+1, N)):
            if C[j][1] < C[j-1][1]:
                C[j], C[j-1] = C[j-1], C[j]
    return C

def selection_sort(A, N):
    C = list(A)
    for i in xrange(N):
        minj = i
        for j in xrange(i, N):
            if C[j][1] < C[minj][1]:
                minj = j
        C[i], C[minj] = C[minj], C[i]
    return C

def is_stable(a, c, n):
    for i in xrange(n-1):
        if c[i][1] == c[i+1][1]:
            if a.index(c[i]) > a.index(c[i+1]):
                print "Not stable"
                return False
    print "Stable"
    return True

def print_c(c):
    print " ".join(map(lambda c:c[0] + str(c[1]), c))

def main():
    n = input()
    c = map(lambda c:(c[0], int(c[1:])), raw_input().split())
    bubble = bubble_sort(c, n)
    selection = selection_sort(c, n)
    print_c(bubble)
    is_stable(c, bubble, n)
    print_c(selection)
    is_stable(c, selection, n)

main()