def compare_cards(a, b):
    return a[1] < b[1]

def bubble_sort(A):
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j]

def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i]

def check_stable(originA, sortedA):
    for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
    return "Stable"

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

bubbleA = list(A)
selectionA = list(A)

bubble_sort(bubbleA)
selection_sort(selectionA)

print(*bubbleA)
print(check_stable(A, bubbleA))

print(*selectionA)
print(check_stable(A, selectionA)) def compare_cards(a, b):
    return a[1] < b[1] compare_cards return a[1] < b[1] a[1] < b[1] a[1] a 1 b[1] b 1 a a b b def bubble_sort(A):
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] bubble_sort for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] i range(len(A)) range len(A) len A for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] j range(len(A)-1, i, -1) range len(A)-1 len(A) len A 1 i -1 if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] compare_cards(A[j], A[j-1]) compare_cards A[j] A j A[j-1] A j-1 j 1 A[j], A[j-1] = A[j-1], A[j] A[j] A j A[j-1] A j-1 j 1 A[j-1] A j-1 j 1 A[j] A j A A def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i] selection_sort for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i] i range(len(A)) range len(A) len A mini = i mini i for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
         j range(i, len(A)) range i len(A) len A if compare_cards(A[j], A[mini]):
                mini = j
         compare_cards(A[j], A[mini]) compare_cards A[j] A j A[mini] A mini mini = j mini j A[i], A[mini] = A[mini], A[i] A[i] A i A[mini] A mini A[mini] A mini A[i] A i A A def check_stable(originA, sortedA):
    for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
    return "Stable" check_stable for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
     i range(len(originA)-1) range len(originA)-1 len(originA) len originA 1 if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
     not compare_cards(sortedA[i], sortedA[i+1]) compare_cards(sortedA[i], sortedA[i+1]) compare_cards sortedA[i] sortedA i sortedA[i+1] sortedA i+1 i 1 if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
     originA.index(sortedA[i]) > originA.index(sortedA[i+1]) originA.index(sortedA[i]) originA.index originA index sortedA[i] sortedA i originA.index(sortedA[i+1]) originA.index originA index sortedA[i+1] sortedA i+1 i 1 return "Not stable" "Not stable" return "Stable" "Stable" originA originA sortedA sortedA N = int(input()) N int(input()) int input() input A = input().split() A input().split() ().split () input split bubbleA = list(A) bubbleA list(A) list A selectionA = list(A) selectionA list(A) list A bubble_sort(bubbleA) bubble_sort bubbleA selection_sort(selectionA) selection_sort selectionA print(*bubbleA) print *bubbleA bubbleA print(check_stable(A, bubbleA)) print check_stable(A, bubbleA) check_stable A bubbleA print(*selectionA) print *selectionA selectionA print(check_stable(A, selectionA)) print check_stable(A, selectionA) check_stable A selectionA def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i] def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i] selection_sort N = int(input()) int(input()) N def bubble_sort(A):
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] def bubble_sort(A):
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j] bubble_sort selectionA = list(A) list(A) selectionA def check_stable(originA, sortedA):
    for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
    return "Stable" def check_stable(originA, sortedA):
    for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
    return "Stable" check_stable bubbleA = list(A) list(A) bubbleA def compare_cards(a, b):
    return a[1] < b[1] def compare_cards(a, b):
    return a[1] < b[1] compare_cards A = input().split() input().split() A
def compare_cards(a, b):
    return a[1] < b[1]

def bubble_sort(A):
    for i in range(len(A)):
        for j in range(len(A)-1, i, -1):
            if compare_cards(A[j], A[j-1]):
                A[j], A[j-1] = A[j-1], A[j]

def selection_sort(A):
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if compare_cards(A[j], A[mini]):
                mini = j
        A[i], A[mini] = A[mini], A[i]

def check_stable(originA, sortedA):
    for i in range(len(originA)-1):
        if not compare_cards(sortedA[i], sortedA[i+1]):
            if originA.index(sortedA[i]) > originA.index(sortedA[i+1]):
                return "Not stable"
    return "Stable"

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

bubbleA = list(A)
selectionA = list(A)

bubble_sort(bubbleA)
selection_sort(selectionA)

print(*bubbleA)
print(check_stable(A, bubbleA))

print(*selectionA)
print(check_stable(A, selectionA))