import copy

def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def val(str):
    return int(str[1])

def is_stable(_in, _out, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
    return True

def print_stable(_in, _out, n):
    if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")    

def find_minj(A, i, n):
    minj = i
    for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
    return minj        

def bubble_sort(A, n):
    A0 = copy.copy(A)
    flg = 1 #逆の隣接要素が存在する
    i = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
    print_list(A)
    print_stable(A0, A, n)

def selection_sort(A, n):
    A0 = copy.copy(A)
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
    print_list(A)
    print_stable(A0, A, n)

n = int(input())
A = list(map(str,input().split()))
B = copy.copy(A)

bubble_sort(A, n)
selection_sort(B, n) import copy copy def print_list(A):
    print(*A, sep=" ") print_list print(*A, sep=" ") print *A A sep=" " sep " " A A def swap(a, b):
    return b, a swap return b, a return b, a b a a a b b def val(str):
    return int(str[1]) val return int(str[1]) int(str[1]) int str[1] str 1 str str def is_stable(_in, _out, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
    return True is_stable for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
     i range(0, n) range 0 n for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
     j range(i+1, n) range i+1 i 1 n for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
     a range(0, n) range 0 n for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
     b range(a+1, n) range a+1 a 1 n if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
     val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a] val(_in[i]) == val(_in[j]) and _in[i] == _out[b] val(_in[i]) == val(_in[j]) val(_in[i]) val _in[i] _in i val(_in[j]) val _in[j] _in j _in[i] == _out[b] _in[i] _in i _out[b] _out b _in[j] == _out[a] _in[j] _in j _out[a] _out a return False False return True True _in _in _out _out n n def print_stable(_in, _out, n):
    if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")     print_stable if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")     is_stable(_in, _out, n) is_stable _in _out n print("Stable") print "Stable" print("Not stable") print "Not stable" _in _in _out _out n n def find_minj(A, i, n):
    minj = i
    for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
    return minj         find_minj minj = i minj i for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
     j range(i, n) range i n if val(A[j]) < val(A[minj]):
            minj = j
     val(A[j]) < val(A[minj]) val(A[j]) val A[j] A j val(A[minj]) val A[minj] A minj minj = j minj j return minj minj A A i i n n def bubble_sort(A, n):
    A0 = copy.copy(A)
    flg = 1 #逆の隣接要素が存在する
    i = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
    print_list(A)
    print_stable(A0, A, n) bubble_sort A0 = copy.copy(A) A0 copy.copy(A) copy.copy copy copy A flg = 1 flg 1 i = 0 i 0 while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
     flg flg = 0 flg 0 for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
         j range(n-1, i, -1) range n-1 n 1 i -1 if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
         val(A[j-1]) > val(A[j]) val(A[j-1]) val A[j-1] A j-1 j 1 val(A[j]) val A[j] A j A[j-1], A[j] = swap(A[j-1], A[j]) A[j-1] A j-1 j 1 swap(A[j-1], A[j]) swap A[j-1] A j-1 j 1 A[j] A j A[j] A j flg = 1 flg 1 i += 1 i 1 print_list(A) print_list A print_stable(A0, A, n) print_stable A0 A n A A n n def selection_sort(A, n):
    A0 = copy.copy(A)
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
    print_list(A)
    print_stable(A0, A, n) selection_sort A0 = copy.copy(A) A0 copy.copy(A) copy.copy copy copy A for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
     i range(0, n) range 0 n minj = find_minj(A, i, n) minj find_minj(A, i, n) find_minj A i n if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
     val(A[i]) > val(A[minj]) val(A[i]) val A[i] A i val(A[minj]) val A[minj] A minj A[i], A[minj] = swap(A[i], A[minj]) A[i] A i swap(A[i], A[minj]) swap A[i] A i A[minj] A minj A[minj] A minj print_list(A) print_list A print_stable(A0, A, n) print_stable A0 A n A A n n n = int(input()) n int(input()) int input() input A = list(map(str,input().split())) A list(map(str,input().split())) list map(str,input().split()) map str input().split() ().split () input split B = copy.copy(A) B copy.copy(A) copy.copy copy copy A bubble_sort(A, n) bubble_sort A n selection_sort(B, n) selection_sort B n n = int(input()) int(input()) n def print_stable(_in, _out, n):
    if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")     def print_stable(_in, _out, n):
    if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")     print_stable def print_list(A):
    print(*A, sep=" ") def print_list(A):
    print(*A, sep=" ") print_list def find_minj(A, i, n):
    minj = i
    for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
    return minj         def find_minj(A, i, n):
    minj = i
    for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
    return minj         find_minj A = list(map(str,input().split())) list(map(str,input().split())) A B = copy.copy(A) copy.copy(A) B def is_stable(_in, _out, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
    return True def is_stable(_in, _out, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
    return True is_stable def bubble_sort(A, n):
    A0 = copy.copy(A)
    flg = 1 #逆の隣接要素が存在する
    i = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
    print_list(A)
    print_stable(A0, A, n) def bubble_sort(A, n):
    A0 = copy.copy(A)
    flg = 1 #逆の隣接要素が存在する
    i = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
    print_list(A)
    print_stable(A0, A, n) bubble_sort def swap(a, b):
    return b, a def swap(a, b):
    return b, a swap def selection_sort(A, n):
    A0 = copy.copy(A)
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
    print_list(A)
    print_stable(A0, A, n) def selection_sort(A, n):
    A0 = copy.copy(A)
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
    print_list(A)
    print_stable(A0, A, n) selection_sort def val(str):
    return int(str[1]) def val(str):
    return int(str[1]) val
import copy

def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def val(str):
    return int(str[1])

def is_stable(_in, _out, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if val(_in[i]) == val(_in[j]) and _in[i] == _out[b] and _in[j] == _out[a]:
                        return False
    return True

def print_stable(_in, _out, n):
    if is_stable(_in, _out, n):
        print("Stable")
    else:
        print("Not stable")    

def find_minj(A, i, n):
    minj = i
    for j in range(i, n):
        if val(A[j]) < val(A[minj]):
            minj = j
    return minj        

def bubble_sort(A, n):
    A0 = copy.copy(A)
    flg = 1 #逆の隣接要素が存在する
    i = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if val(A[j-1]) > val(A[j]):
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
        i += 1
    print_list(A)
    print_stable(A0, A, n)

def selection_sort(A, n):
    A0 = copy.copy(A)
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if val(A[i]) > val(A[minj]):
            A[i], A[minj] = swap(A[i], A[minj])
    print_list(A)
    print_stable(A0, A, n)

n = int(input())
A = list(map(str,input().split()))
B = copy.copy(A)

bubble_sort(A, n)
selection_sort(B, n)
