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


def conv_input(arr):
    ret = []
    for e in arr:
        ret.append((e[0], int(e[1])))
    return ret


def conv_output(conv_arr):
    ret = []
    for e in conv_arr:
        ret.append(e[0] + str(e[1]))
    return ret


def is_stable(arr1, arr2):
    ret = 'Stable'
    for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
    return ret


def bubble_sort(n, arr):
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
    return arr


def selection_sort(n, arr):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
    return arr

if __name__ == '__main__':
    n = int(input())
    arr1 = arr2 = [str(s) for s in input().split()]
    arr1 = conv_output(bubble_sort(n, conv_input(arr1)))
    print(' '.join(map(str, arr1)))
    print('Stable')
    arr2 = conv_output(selection_sort(n, conv_input(arr2)))
    print(' '.join(map(str, arr2)))
    print(is_stable(arr1, arr2)) def conv_input(arr):
    ret = []
    for e in arr:
        ret.append((e[0], int(e[1])))
    return ret conv_input ret = [] ret [] for e in arr:
        ret.append((e[0], int(e[1])))
     e arr ret.append((e[0], int(e[1]))) ret.append ret append (e[0], int(e[1])) e[0] e 0 int(e[1]) int e[1] e 1 return ret ret arr arr def conv_output(conv_arr):
    ret = []
    for e in conv_arr:
        ret.append(e[0] + str(e[1]))
    return ret conv_output ret = [] ret [] for e in conv_arr:
        ret.append(e[0] + str(e[1]))
     e conv_arr ret.append(e[0] + str(e[1])) ret.append ret append e[0] + str(e[1]) e[0] e 0 str(e[1]) str e[1] e 1 return ret ret conv_arr conv_arr def is_stable(arr1, arr2):
    ret = 'Stable'
    for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
    return ret is_stable ret = 'Stable' ret 'Stable' for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
     a1 a2 zip(arr1, arr2) zip arr1 arr2 if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
     a1 == a2 a1 a2 continue ret = 'Not stable' ret 'Not stable' break return ret ret arr1 arr1 arr2 arr2 def bubble_sort(n, arr):
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
    return arr bubble_sort flag = True flag True while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
     flag flag = False flag False for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
     i reversed(range(n)) reversed range(n) range n if i is 0:
                break
             i is 0 i 0 break if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
     arr[i][1] < arr[i - 1][1] arr[i][1] [i] arr i 1 arr[i - 1][1] [i - 1] arr i - 1 i 1 1 tmp = arr[i] tmp arr[i] arr i arr[i] = arr[i - 1] arr[i] arr i arr[i - 1] arr i - 1 i 1 arr[i - 1] = tmp arr[i - 1] arr i - 1 i 1 tmp flag = True flag True return arr arr n n arr arr def selection_sort(n, arr):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
    return arr selection_sort for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
     i range(n) range n minj = i minj i for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
         j range(i, n) range i n if arr[j][1] < arr[minj][1]:
                minj = j
         arr[j][1] < arr[minj][1] arr[j][1] [j] arr j 1 arr[minj][1] [minj] arr minj 1 minj = j minj j if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
     i != minj i minj tmp = arr[minj] tmp arr[minj] arr minj arr[minj] = arr[i] arr[minj] arr minj arr[i] arr i arr[i] = tmp arr[i] arr i tmp return arr arr n n arr arr if __name__ == '__main__':
    n = int(input())
    arr1 = arr2 = [str(s) for s in input().split()]
    arr1 = conv_output(bubble_sort(n, conv_input(arr1)))
    print(' '.join(map(str, arr1)))
    print('Stable')
    arr2 = conv_output(selection_sort(n, conv_input(arr2)))
    print(' '.join(map(str, arr2)))
    print(is_stable(arr1, arr2)) __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input str(s) for s in input().split() for s in input().split() s input().split() ().split () input split for s in input().split() str(s) str s arr1 = arr2 = [str(s) for s in input().split()] arr1 [str(s) for s in input().split()] = arr2 = [str(s) for s in input().split()] arr2 [str(s) for s in input().split()] arr1 = conv_output(bubble_sort(n, conv_input(arr1))) arr1 conv_output(bubble_sort(n, conv_input(arr1))) conv_output bubble_sort(n, conv_input(arr1)) bubble_sort n conv_input(arr1) conv_input arr1 print(' '.join(map(str, arr1))) print ' '.join(map(str, arr1)) ' '.join ' ' join map(str, arr1) map str arr1 print('Stable') print 'Stable' arr2 = conv_output(selection_sort(n, conv_input(arr2))) arr2 conv_output(selection_sort(n, conv_input(arr2))) conv_output selection_sort(n, conv_input(arr2)) selection_sort n conv_input(arr2) conv_input arr2 print(' '.join(map(str, arr2))) print ' '.join(map(str, arr2)) ' '.join ' ' join map(str, arr2) map str arr2 print(is_stable(arr1, arr2)) print is_stable(arr1, arr2) is_stable arr1 arr2 def conv_output(conv_arr):
    ret = []
    for e in conv_arr:
        ret.append(e[0] + str(e[1]))
    return ret def conv_output(conv_arr):
    ret = []
    for e in conv_arr:
        ret.append(e[0] + str(e[1]))
    return ret conv_output arr2 = conv_output(selection_sort(n, conv_input(arr2))) conv_output(selection_sort(n, conv_input(arr2))) arr2 def selection_sort(n, arr):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
    return arr def selection_sort(n, arr):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
    return arr selection_sort arr2 = [str(s) for s in input().split()] [str(s) for s in input().split()] arr2 def conv_input(arr):
    ret = []
    for e in arr:
        ret.append((e[0], int(e[1])))
    return ret def conv_input(arr):
    ret = []
    for e in arr:
        ret.append((e[0], int(e[1])))
    return ret conv_input n = int(input()) int(input()) n def is_stable(arr1, arr2):
    ret = 'Stable'
    for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
    return ret def is_stable(arr1, arr2):
    ret = 'Stable'
    for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
    return ret is_stable arr1 = arr2 = [str(s) for s in input().split()] [str(s) for s in input().split()] arr1 def bubble_sort(n, arr):
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
    return arr def bubble_sort(n, arr):
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
    return arr bubble_sort arr1 = conv_output(bubble_sort(n, conv_input(arr1))) conv_output(bubble_sort(n, conv_input(arr1))) arr1
# -*- coding: utf-8 -*-


def conv_input(arr):
    ret = []
    for e in arr:
        ret.append((e[0], int(e[1])))
    return ret


def conv_output(conv_arr):
    ret = []
    for e in conv_arr:
        ret.append(e[0] + str(e[1]))
    return ret


def is_stable(arr1, arr2):
    ret = 'Stable'
    for a1, a2 in zip(arr1, arr2):
        if a1 == a2:
            continue
        else:
            ret = 'Not stable'
            break
    return ret


def bubble_sort(n, arr):
    flag = True
    while flag:
        flag = False
        for i in reversed(range(n)):
            if i is 0:
                break
            if arr[i][1] < arr[i - 1][1]:
                tmp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = tmp
                flag = True
    return arr


def selection_sort(n, arr):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if arr[j][1] < arr[minj][1]:
                minj = j
        if i != minj:
            tmp = arr[minj]
            arr[minj] = arr[i]
            arr[i] = tmp
    return arr

if __name__ == '__main__':
    n = int(input())
    arr1 = arr2 = [str(s) for s in input().split()]
    arr1 = conv_output(bubble_sort(n, conv_input(arr1)))
    print(' '.join(map(str, arr1)))
    print('Stable')
    arr2 = conv_output(selection_sort(n, conv_input(arr2)))
    print(' '.join(map(str, arr2)))
    print(is_stable(arr1, arr2))