import copy


def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return


def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return


def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable'


n = int(input())
arr = input().split()
tar = []

for s in arr:
    tar.append({'crd': s[0], 'val': s[1], 'str': s})

bub = copy.copy(tar)
BubleSort(bub, n)

prt = []
for s in bub:
    prt.append(s['str'])
print(*prt)
print(isStable(tar, bub, n))

bub = copy.copy(tar)
SelectionSort(bub, n)

prt = []
for s in bub:
    prt.append(s['str'])
print(*prt)
print(isStable(tar, bub, n))
 import copy copy def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return SelectionSort for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
     i range(n) range n mini = i mini i for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
         j range(i, n) range i n if arr[j]['val'] < arr[mini]['val']:
                mini = j
         arr[j]['val'] < arr[mini]['val'] arr[j]['val'] [j] arr j 'val' arr[mini]['val'] [mini] arr mini 'val' mini = j mini j if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
     mini != i mini i arr[i], arr[mini] = arr[mini], arr[i] arr[i] arr i arr[mini] arr mini arr[mini] arr mini arr[i] arr i return arr arr n n def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return BubleSort for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
     i range(n) range n for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
     j range(n - 1, i, -1) range n - 1 n 1 i -1 if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
     arr[j]['val'] < arr[j-1]['val'] arr[j]['val'] [j] arr j 'val' arr[j-1]['val'] [j-1] arr j-1 j 1 'val' arr[j], arr[j-1] = arr[j-1], arr[j] arr[j] arr j arr[j-1] arr j-1 j 1 arr[j-1] arr j-1 j 1 arr[j] arr j return arr arr n n def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable' isStable i = 0 i 0 while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
     i < n i n tmpVal = comparr[i]['val'] tmpVal comparr[i]['val'] [i] comparr i 'val' for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
         j range(n) range n if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
         inarr[j]['val'] == tmpVal inarr[j]['val'] [j] inarr j 'val' tmpVal if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
         inarr[j]['crd'] == comparr[i]['crd'] inarr[j]['crd'] [j] inarr j 'crd' comparr[i]['crd'] [i] comparr i 'crd' if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                 i != n - 1 and comparr[i+1]['val'] == tmpVal i != n - 1 i n - 1 n 1 comparr[i+1]['val'] == tmpVal comparr[i+1]['val'] [i+1] comparr i+1 i 1 'val' tmpVal i += 1 i 1 break return 'Not stable' 'Not stable' i += 1 i 1 return 'Stable' 'Stable' inarr inarr comparr comparr n n n = int(input()) n int(input()) int input() input arr = input().split() arr input().split() ().split () input split tar = [] tar [] for s in arr:
    tar.append({'crd': s[0], 'val': s[1], 'str': s}) s arr tar.append({'crd': s[0], 'val': s[1], 'str': s}) tar.append tar append {'crd': s[0], 'val': s[1], 'str': s} 'crd' 'crd' s[0] s 0 'val' 'val' s[1] s 1 'str' 'str' s bub = copy.copy(tar) bub copy.copy(tar) copy.copy copy copy tar BubleSort(bub, n) BubleSort bub n prt = [] prt [] for s in bub:
    prt.append(s['str']) s bub prt.append(s['str']) prt.append prt append s['str'] s 'str' print(*prt) print *prt prt print(isStable(tar, bub, n)) print isStable(tar, bub, n) isStable tar bub n bub = copy.copy(tar) bub copy.copy(tar) copy.copy copy copy tar SelectionSort(bub, n) SelectionSort bub n prt = [] prt [] for s in bub:
    prt.append(s['str']) s bub prt.append(s['str']) prt.append prt append s['str'] s 'str' print(*prt) print *prt prt print(isStable(tar, bub, n)) print isStable(tar, bub, n) isStable tar bub n tar = [] [] tar bub = copy.copy(tar) copy.copy(tar) bub def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return SelectionSort prt = [] [] prt n = int(input()) int(input()) n prt = [] [] prt def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return BubleSort def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable' def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable' isStable arr = input().split() input().split() arr bub = copy.copy(tar) copy.copy(tar) bub
import copy


def SelectionSort(arr, n):
    for i in range(n):
        mini = i
        for j in range(i, n):
            if arr[j]['val'] < arr[mini]['val']:
                mini = j
        if mini != i:
            arr[i], arr[mini] = arr[mini], arr[i]
    return


def BubleSort(arr, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if arr[j]['val'] < arr[j-1]['val']:
                arr[j], arr[j-1] = arr[j-1], arr[j]
    return


def isStable(inarr, comparr, n):
    i = 0
    while i < n:
        tmpVal = comparr[i]['val']
        for j in range(n):
            if inarr[j]['val'] == tmpVal:
                if inarr[j]['crd'] == comparr[i]['crd']:
                    if i != n - 1 and comparr[i+1]['val'] == tmpVal:
                        i += 1
                    else:
                        break
                else:
                    return 'Not stable'
        i += 1
    return 'Stable'


n = int(input())
arr = input().split()
tar = []

for s in arr:
    tar.append({'crd': s[0], 'val': s[1], 'str': s})

bub = copy.copy(tar)
BubleSort(bub, n)

prt = []
for s in bub:
    prt.append(s['str'])
print(*prt)
print(isStable(tar, bub, n))

bub = copy.copy(tar)
SelectionSort(bub, n)

prt = []
for s in bub:
    prt.append(s['str'])
print(*prt)
print(isStable(tar, bub, n))

