# -*- coding: utf-8 -*-


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

def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c

input_num = int(input())
input_list = input().split()
bubble_list = bubble_sort(input_list)
selection_list = selection_sort(input_list)
for c in bubble_list:
    print(c, end='')
    if c != bubble_list[len(bubble_list)-1]:
        print(' ', end='')
print("\nStable")
stable_flg = True
for i in range(len(input_list)):
    if bubble_list[i] != selection_list[i]:
        stable_flg = False
    print(selection_list[i], end='')
    if i < len(input_list)-1:
        print(" ", end='')
if stable_flg:
    print("\nStable")
else:
    print("\nNot stable") def bubble_sort(c):
    c = list(c)
    for i in range(len(c)):
        for j in range(len(c)-1, i, -1):
            if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
    return c bubble_sort c = list(c) c list(c) list c for i in range(len(c)):
        for j in range(len(c)-1, i, -1):
            if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
     i range(len(c)) range len(c) len c for j in range(len(c)-1, i, -1):
            if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
     j range(len(c)-1, i, -1) range len(c)-1 len(c) len c 1 i -1 if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
     int(c[j][1]) < int(c[j-1][1]) int(c[j][1]) int c[j][1] [j] c j 1 int(c[j-1][1]) int 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 c c def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c selection_sort c = list(c) c list(c) list c for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
     i range(len(c)) range len(c) len c minj = i minj i for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
         j range(i+1, len(c)) range i+1 i 1 len(c) len c if int(c[j][1]) < int(c[minj][1]):
                minj = j
         int(c[j][1]) < int(c[minj][1]) int(c[j][1]) int c[j][1] [j] c j 1 int(c[minj][1]) int 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 c c input_num = int(input()) input_num int(input()) int input() input input_list = input().split() input_list input().split() ().split () input split bubble_list = bubble_sort(input_list) bubble_list bubble_sort(input_list) bubble_sort input_list selection_list = selection_sort(input_list) selection_list selection_sort(input_list) selection_sort input_list for c in bubble_list:
    print(c, end='')
    if c != bubble_list[len(bubble_list)-1]:
        print(' ', end='') c bubble_list print(c, end='') print c end='' end '' if c != bubble_list[len(bubble_list)-1]:
        print(' ', end='') c != bubble_list[len(bubble_list)-1] c bubble_list[len(bubble_list)-1] bubble_list len(bubble_list)-1 len(bubble_list) len bubble_list 1 print(' ', end='') print ' ' end='' end '' print("\nStable") print "\nStable" stable_flg = True stable_flg True for i in range(len(input_list)):
    if bubble_list[i] != selection_list[i]:
        stable_flg = False
    print(selection_list[i], end='')
    if i < len(input_list)-1:
        print(" ", end='') i range(len(input_list)) range len(input_list) len input_list if bubble_list[i] != selection_list[i]:
        stable_flg = False
     bubble_list[i] != selection_list[i] bubble_list[i] bubble_list i selection_list[i] selection_list i stable_flg = False stable_flg False print(selection_list[i], end='') print selection_list[i] selection_list i end='' end '' if i < len(input_list)-1:
        print(" ", end='') i < len(input_list)-1 i len(input_list)-1 len(input_list) len input_list 1 print(" ", end='') print " " end='' end '' if stable_flg:
    print("\nStable")
else:
    print("\nNot stable") stable_flg print("\nStable") print "\nStable" print("\nNot stable") print "\nNot stable" def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c selection_sort input_list = input().split() input().split() input_list selection_list = selection_sort(input_list) selection_sort(input_list) selection_list def bubble_sort(c):
    c = list(c)
    for i in range(len(c)):
        for j in range(len(c)-1, i, -1):
            if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
    return c def bubble_sort(c):
    c = list(c)
    for i in range(len(c)):
        for j in range(len(c)-1, i, -1):
            if int(c[j][1]) < int(c[j-1][1]):
                c[j], c[j-1] = c[j-1], c[j]
    return c bubble_sort input_num = int(input()) int(input()) input_num bubble_list = bubble_sort(input_list) bubble_sort(input_list) bubble_list stable_flg = True True stable_flg stable_flg = False False stable_flg
# -*- coding: utf-8 -*-


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

def selection_sort(c):
    c = list(c)
    for i in range(len(c)):
        minj = i
        for j in range(i+1, len(c)):
            if int(c[j][1]) < int(c[minj][1]):
                minj = j
        c[i], c[minj] = c[minj], c[i]
    return c

input_num = int(input())
input_list = input().split()
bubble_list = bubble_sort(input_list)
selection_list = selection_sort(input_list)
for c in bubble_list:
    print(c, end='')
    if c != bubble_list[len(bubble_list)-1]:
        print(' ', end='')
print("\nStable")
stable_flg = True
for i in range(len(input_list)):
    if bubble_list[i] != selection_list[i]:
        stable_flg = False
    print(selection_list[i], end='')
    if i < len(input_list)-1:
        print(" ", end='')
if stable_flg:
    print("\nStable")
else:
    print("\nNot stable")