import copy

N = int(input())
A = list(input().split())

def is_stable(sorted_list):
    for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
    return True

def print_stable_or_not(is_stable):
    if is_stable:
        print("Stable")
    else:
        print("Not stable")

def bubble_sort(A1):
    flg = True
    while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
    print(" ".join(A1))
    print_stable_or_not(is_stable(A1))

def insertion_sort(A2):
    for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
    print(" ".join(A2))
    print_stable_or_not(is_stable(A2))

if __name__ == '__main__':
    A1 = copy.deepcopy(A)
    A2 = copy.deepcopy(A)
    bubble_sort(A1)
    insertion_sort(A2) import copy copy N = int(input()) N int(input()) int input() input A = list(input().split()) A list(input().split()) list input().split() ().split () input split def is_stable(sorted_list):
    for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
    return True is_stable for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
     i range(len(sorted_list)-1) range len(sorted_list)-1 len(sorted_list) len sorted_list 1 if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
     sorted_list[i][1] == sorted_list[i+1][1] sorted_list[i][1] [i] sorted_list i 1 sorted_list[i+1][1] [i+1] sorted_list i+1 i 1 1 for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
             j range(len(A)) range len(A) len A if A[j] == sorted_list[i]:
                    smaller_idx = j
                 A[j] == sorted_list[i] A[j] A j sorted_list[i] sorted_list i smaller_idx = j smaller_idx j if A[j] == sorted_list[i+1]:
                    larger_idx = j
             A[j] == sorted_list[i+1] A[j] A j sorted_list[i+1] sorted_list i+1 i 1 larger_idx = j larger_idx j if smaller_idx > larger_idx:
                return False
     smaller_idx > larger_idx smaller_idx larger_idx return False False return True True sorted_list sorted_list def print_stable_or_not(is_stable):
    if is_stable:
        print("Stable")
    else:
        print("Not stable") print_stable_or_not if is_stable:
        print("Stable")
    else:
        print("Not stable") is_stable print("Stable") print "Stable" print("Not stable") print "Not stable" is_stable is_stable def bubble_sort(A1):
    flg = True
    while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
    print(" ".join(A1))
    print_stable_or_not(is_stable(A1)) bubble_sort flg = True flg True while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
     flg for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
     i range(N) range N flg = False flg False for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
     j range(N-1, i, -1) range N-1 N 1 i -1 if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
     int(A1[j-1][1]) > int(A1[j][1]) int(A1[j-1][1]) int A1[j-1][1] [j-1] A1 j-1 j 1 1 int(A1[j][1]) int A1[j][1] [j] A1 j 1 A1[j-1], A1[j] = A1[j], A1[j-1] A1[j-1] A1 j-1 j 1 A1[j] A1 j A1[j] A1 j A1[j-1] A1 j-1 j 1 flg = True flg True print(" ".join(A1)) print " ".join(A1) " ".join " " join A1 print_stable_or_not(is_stable(A1)) print_stable_or_not is_stable(A1) is_stable A1 A1 A1 def insertion_sort(A2):
    for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
    print(" ".join(A2))
    print_stable_or_not(is_stable(A2)) insertion_sort for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
     i range(N-1) range N-1 N 1 min_idx = i min_idx i for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
         j range(i+1, N) range i+1 i 1 N if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
         int(A2[min_idx][1]) > int(A2[j][1]) int(A2[min_idx][1]) int A2[min_idx][1] [min_idx] A2 min_idx 1 int(A2[j][1]) int A2[j][1] [j] A2 j 1 min_idx = j min_idx j if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
     min_idx != i min_idx i A2[i], A2[min_idx] = A2[min_idx], A2[i] A2[i] A2 i A2[min_idx] A2 min_idx A2[min_idx] A2 min_idx A2[i] A2 i print(" ".join(A2)) print " ".join(A2) " ".join " " join A2 print_stable_or_not(is_stable(A2)) print_stable_or_not is_stable(A2) is_stable A2 A2 A2 if __name__ == '__main__':
    A1 = copy.deepcopy(A)
    A2 = copy.deepcopy(A)
    bubble_sort(A1)
    insertion_sort(A2) __name__ == '__main__' __name__ '__main__' A1 = copy.deepcopy(A) A1 copy.deepcopy(A) copy.deepcopy copy deepcopy A A2 = copy.deepcopy(A) A2 copy.deepcopy(A) copy.deepcopy copy deepcopy A bubble_sort(A1) bubble_sort A1 insertion_sort(A2) insertion_sort A2 def print_stable_or_not(is_stable):
    if is_stable:
        print("Stable")
    else:
        print("Not stable") def print_stable_or_not(is_stable):
    if is_stable:
        print("Stable")
    else:
        print("Not stable") print_stable_or_not def bubble_sort(A1):
    flg = True
    while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
    print(" ".join(A1))
    print_stable_or_not(is_stable(A1)) def bubble_sort(A1):
    flg = True
    while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
    print(" ".join(A1))
    print_stable_or_not(is_stable(A1)) bubble_sort N = int(input()) int(input()) N def is_stable(sorted_list):
    for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
    return True def is_stable(sorted_list):
    for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
    return True is_stable def insertion_sort(A2):
    for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
    print(" ".join(A2))
    print_stable_or_not(is_stable(A2)) def insertion_sort(A2):
    for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
    print(" ".join(A2))
    print_stable_or_not(is_stable(A2)) insertion_sort A2 = copy.deepcopy(A) copy.deepcopy(A) A2 A = list(input().split()) list(input().split()) A A1 = copy.deepcopy(A) copy.deepcopy(A) A1
import copy

N = int(input())
A = list(input().split())

def is_stable(sorted_list):
    for i in range(len(sorted_list)-1):
        if sorted_list[i][1] == sorted_list[i+1][1]:
            for j in range(len(A)):
                if A[j] == sorted_list[i]:
                    smaller_idx = j
                if A[j] == sorted_list[i+1]:
                    larger_idx = j
            if smaller_idx > larger_idx:
                return False
    return True

def print_stable_or_not(is_stable):
    if is_stable:
        print("Stable")
    else:
        print("Not stable")

def bubble_sort(A1):
    flg = True
    while flg:
        for i in range(N):
            flg = False
            for j in range(N-1, i, -1):
                if int(A1[j-1][1]) > int(A1[j][1]):
                    A1[j-1], A1[j] = A1[j], A1[j-1]
                    flg = True
    print(" ".join(A1))
    print_stable_or_not(is_stable(A1))

def insertion_sort(A2):
    for i in range(N-1):
        min_idx = i
        for j in range(i+1, N):
            if int(A2[min_idx][1]) > int(A2[j][1]):
                min_idx = j
        if min_idx != i:
            A2[i], A2[min_idx] = A2[min_idx], A2[i]
    print(" ".join(A2))
    print_stable_or_not(is_stable(A2))

if __name__ == '__main__':
    A1 = copy.deepcopy(A)
    A2 = copy.deepcopy(A)
    bubble_sort(A1)
    insertion_sort(A2)
