#coding:utf-8
#1_2_C

def BubbleSort(cards, n):
    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):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

n = int(input())
cardsR = input().split()
cardsS = cardsR[:]

print(' '.join(BubbleSort(cardsR, n)))
print("Stable")
print(*SelectionSort(cardsS, n))
print("Stable" if cardsR == cardsS else "Not stable") def BubbleSort(cards, n):
    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 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 n n def SelectionSort(cards, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards SelectionSort for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][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[minj][1] > cards[j][1]:
                minj = j
         j range(i, n) range i n if cards[minj][1] > cards[j][1]:
                minj = j
         cards[minj][1] > cards[j][1] cards[minj][1] [minj] cards minj 1 cards[j][1] [j] cards j 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 n n n = int(input()) n int(input()) int input() input cardsR = input().split() cardsR input().split() ().split () input split cardsS = cardsR[:] cardsS cardsR[:] cardsR : print(' '.join(BubbleSort(cardsR, n))) print ' '.join(BubbleSort(cardsR, n)) ' '.join ' ' join BubbleSort(cardsR, n) BubbleSort cardsR n print("Stable") print "Stable" print(*SelectionSort(cardsS, n)) print *SelectionSort(cardsS, n) SelectionSort(cardsS, n) SelectionSort cardsS n print("Stable" if cardsR == cardsS else "Not stable") print "Stable" if cardsR == cardsS else "Not stable" cardsR == cardsS cardsR cardsS "Stable" "Not stable" def BubbleSort(cards, n):
    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):
    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 def SelectionSort(cards, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards def SelectionSort(cards, n):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards SelectionSort n = int(input()) int(input()) n cardsR = input().split() input().split() cardsR cardsS = cardsR[:] cardsR[:] cardsS
#coding:utf-8
#1_2_C

def BubbleSort(cards, n):
    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):
    for i in range(n):
        minj = i
        for j in range(i, n):
            if cards[minj][1] > cards[j][1]:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

n = int(input())
cardsR = input().split()
cardsS = cardsR[:]

print(' '.join(BubbleSort(cardsR, n)))
print("Stable")
print(*SelectionSort(cardsS, n))
print("Stable" if cardsR == cardsS else "Not stable")