def bubbleSort(cards):
    n = len(cards)
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
    return cards


def selectionSort(cards):
    n = len(cards)
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
    return cards


def isStableSort(initialArray, resultArray):
    n = len(initialArray)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
    return True


n = int(input())
array = [x for x in input().split()]
cards = [[a[0], a[1]] for a in array]
cardsForBubble = cards[:]
cardsForSelection = cards[:]

bSort = bubbleSort(cardsForBubble)
sSort = selectionSort(cardsForSelection)

resultForBubble = []
resultForSelection = []

for b in bSort:
    resultForBubble.append(''.join(b))
for s in sSort:
    resultForSelection.append(''.join(s))

print(*resultForBubble)
if isStableSort(array, resultForBubble):
    print("Stable")
else:
    print("Not stable")

print(*resultForSelection)
if isStableSort(array, resultForSelection):
    print("Stable")
else:
    print("Not stable") def bubbleSort(cards):
    n = len(cards)
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
    return cards bubbleSort n = len(cards) n len(cards) len cards for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
     i range(n) range n for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
     j range(n-1, i, -1) range n-1 n 1 i -1 if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
     cards[j][1] < cards[j-1][1] cards[j][1] [j] cards j 1 cards[j-1][1] [j-1] cards j-1 j 1 1 cards[j], cards[j-1] = cards[j-1], cards[j] cards[j] cards j cards[j-1] cards j-1 j 1 cards[j-1] cards j-1 j 1 cards[j] cards j return cards cards cards cards def selectionSort(cards):
    n = len(cards)
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
    return cards selectionSort n = len(cards) n len(cards) len cards for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
     i range(n) range n minj = i minj i for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
         j range(i, n) range i n if cards[j][1] < cards[minj][1]:
                minj = j
         cards[j][1] < cards[minj][1] cards[j][1] [j] cards j 1 cards[minj][1] [minj] cards minj 1 minj = j minj j cards[i], cards[minj] = cards[minj], cards[i] cards[i] cards i cards[minj] cards minj cards[minj] cards minj cards[i] cards i return cards cards cards cards def isStableSort(initialArray, resultArray):
    n = len(initialArray)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
    return True isStableSort n = len(initialArray) n len(initialArray) len initialArray for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     i range(n) range n for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     j range(i+1, n) range i+1 i 1 n for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     a range(n) range n for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     b range(a+1, n) range a+1 a 1 n if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     initialArray[i][1] == initialArray[j][1] initialArray[i][1] [i] initialArray i 1 initialArray[j][1] [j] initialArray j 1 if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
     initialArray[i] == resultArray[b] initialArray[i] initialArray i resultArray[b] resultArray b if initialArray[j] == resultArray[a]:
                                return False
    
     initialArray[j] == resultArray[a] initialArray[j] initialArray j resultArray[a] resultArray a return False False return True True initialArray initialArray resultArray resultArray n = int(input()) n int(input()) int 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 array = [x for x in input().split()] array [x for x in input().split()] [a[0], a[1]] for a in array for a in array a array for a in array [a[0], a[1]] a[0] a 0 a[1] a 1 cards = [[a[0], a[1]] for a in array] cards [[a[0], a[1]] for a in array] cardsForBubble = cards[:] cardsForBubble cards[:] cards : cardsForSelection = cards[:] cardsForSelection cards[:] cards : bSort = bubbleSort(cardsForBubble) bSort bubbleSort(cardsForBubble) bubbleSort cardsForBubble sSort = selectionSort(cardsForSelection) sSort selectionSort(cardsForSelection) selectionSort cardsForSelection resultForBubble = [] resultForBubble [] resultForSelection = [] resultForSelection [] for b in bSort:
    resultForBubble.append(''.join(b)) b bSort resultForBubble.append(''.join(b)) resultForBubble.append resultForBubble append ''.join(b) ''.join '' join b for s in sSort:
    resultForSelection.append(''.join(s)) s sSort resultForSelection.append(''.join(s)) resultForSelection.append resultForSelection append ''.join(s) ''.join '' join s print(*resultForBubble) print *resultForBubble resultForBubble if isStableSort(array, resultForBubble):
    print("Stable")
else:
    print("Not stable") isStableSort(array, resultForBubble) isStableSort array resultForBubble print("Stable") print "Stable" print("Not stable") print "Not stable" print(*resultForSelection) print *resultForSelection resultForSelection if isStableSort(array, resultForSelection):
    print("Stable")
else:
    print("Not stable") isStableSort(array, resultForSelection) isStableSort array resultForSelection print("Stable") print "Stable" print("Not stable") print "Not stable" array = [x for x in input().split()] [x for x in input().split()] array def selectionSort(cards):
    n = len(cards)
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
    return cards def selectionSort(cards):
    n = len(cards)
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
    return cards selectionSort def bubbleSort(cards):
    n = len(cards)
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
    return cards def bubbleSort(cards):
    n = len(cards)
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
    return cards bubbleSort sSort = selectionSort(cardsForSelection) selectionSort(cardsForSelection) sSort def isStableSort(initialArray, resultArray):
    n = len(initialArray)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
    return True def isStableSort(initialArray, resultArray):
    n = len(initialArray)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
    return True isStableSort cardsForBubble = cards[:] cards[:] cardsForBubble bSort = bubbleSort(cardsForBubble) bubbleSort(cardsForBubble) bSort cards = [[a[0], a[1]] for a in array] [[a[0], a[1]] for a in array] cards resultForSelection = [] [] resultForSelection resultForBubble = [] [] resultForBubble n = int(input()) int(input()) n cardsForSelection = cards[:] cards[:] cardsForSelection
def bubbleSort(cards):
    n = len(cards)
    for i in range(n):
        for j in range(n-1, i, -1):
            if cards[j][1] < cards[j-1][1]:
                cards[j], cards[j-1] = cards[j-1], cards[j]
    
    return cards


def selectionSort(cards):
    n = len(cards)
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[j][1] < cards[minj][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    
    return cards


def isStableSort(initialArray, resultArray):
    n = len(initialArray)
    for i in range(n):
        for j in range(i+1, n):
            for a in range(n):
                for b in range(a+1, n):
                    if initialArray[i][1] == initialArray[j][1]:
                        if initialArray[i] == resultArray[b]:
                            if initialArray[j] == resultArray[a]:
                                return False
    
    return True


n = int(input())
array = [x for x in input().split()]
cards = [[a[0], a[1]] for a in array]
cardsForBubble = cards[:]
cardsForSelection = cards[:]

bSort = bubbleSort(cardsForBubble)
sSort = selectionSort(cardsForSelection)

resultForBubble = []
resultForSelection = []

for b in bSort:
    resultForBubble.append(''.join(b))
for s in sSort:
    resultForSelection.append(''.join(s))

print(*resultForBubble)
if isStableSort(array, resultForBubble):
    print("Stable")
else:
    print("Not stable")

print(*resultForSelection)
if isStableSort(array, resultForSelection):
    print("Stable")
else:
    print("Not stable")
