def bubble_sort(data):
    for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j]

def selection_sort(data):
    for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i]

input()
cards = [x for x in input().split()]
cards_b = cards.copy()
cards_s = cards.copy()
bubble_sort(cards_b)
selection_sort(cards_s)
for data in [cards_b, cards_s]:
    print(' '.join(data))
    if data == cards_b:
        print("Stable")
    else:
        print("Not stable") def bubble_sort(data):
    for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] bubble_sort for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] i range(len(data) - 1) range len(data) - 1 len(data) len data 1 for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] j range(len(data) - 1, i, -1) range len(data) - 1 len(data) len data 1 i -1 if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] int(data[j][1]) < int(data[j - 1][1]) int(data[j][1]) int data[j][1] [j] data j 1 int(data[j - 1][1]) int data[j - 1][1] [j - 1] data j - 1 j 1 1 data[j], data[j - 1] = data[j - 1], data[j] data[j] data j data[j - 1] data j - 1 j 1 data[j - 1] data j - 1 j 1 data[j] data j data data def selection_sort(data):
    for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i] selection_sort for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i] i range(len(data)) range len(data) len data min_j = i min_j i for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
         j range(i, len(data)) range i len(data) len data if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
         int(data[j][1]) < int(data[min_j][1]) int(data[j][1]) int data[j][1] [j] data j 1 int(data[min_j][1]) int data[min_j][1] [min_j] data min_j 1 min_j = j min_j j if min_j != i:
            data[i], data[min_j] = data[min_j], data[i] min_j != i min_j i data[i], data[min_j] = data[min_j], data[i] data[i] data i data[min_j] data min_j data[min_j] data min_j data[i] data i data data input() input x for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() x cards = [x for x in input().split()] cards [x for x in input().split()] cards_b = cards.copy() cards_b cards.copy() cards.copy cards copy cards_s = cards.copy() cards_s cards.copy() cards.copy cards copy bubble_sort(cards_b) bubble_sort cards_b selection_sort(cards_s) selection_sort cards_s for data in [cards_b, cards_s]:
    print(' '.join(data))
    if data == cards_b:
        print("Stable")
    else:
        print("Not stable") data [cards_b, cards_s] cards_b cards_s print(' '.join(data)) print ' '.join(data) ' '.join ' ' join data if data == cards_b:
        print("Stable")
    else:
        print("Not stable") data == cards_b data cards_b print("Stable") print "Stable" print("Not stable") print "Not stable" cards_b = cards.copy() cards.copy() cards_b def bubble_sort(data):
    for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] def bubble_sort(data):
    for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j] bubble_sort cards = [x for x in input().split()] [x for x in input().split()] cards def selection_sort(data):
    for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i] def selection_sort(data):
    for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i] selection_sort cards_s = cards.copy() cards.copy() cards_s
def bubble_sort(data):
    for i in range(len(data) - 1):
        for j in range(len(data) - 1, i, -1):
            if int(data[j][1]) < int(data[j - 1][1]):
                data[j], data[j - 1] = data[j - 1], data[j]

def selection_sort(data):
    for i in range(len(data)):
        min_j = i
        for j in range(i, len(data)):
            if int(data[j][1]) < int(data[min_j][1]):
                min_j = j
        if min_j != i:
            data[i], data[min_j] = data[min_j], data[i]

input()
cards = [x for x in input().split()]
cards_b = cards.copy()
cards_s = cards.copy()
bubble_sort(cards_b)
selection_sort(cards_s)
for data in [cards_b, cards_s]:
    print(' '.join(data))
    if data == cards_b:
        print("Stable")
    else:
        print("Not stable")