
class Card:
    def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i

def BubbleSort(cards, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j]

def SelectionSort(cards, N):
    for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i]

N = int(input())
cards = [Card(data, i) for i, data in enumerate(input().split())]

cards1 = cards.copy()
cards2 = cards.copy()

BubbleSort(cards1, N)
SelectionSort(cards2, N)

print(*[card.suit + card.value for card in cards1])

for i in range(N - 1):
    if cards1[i].value == cards1[i + 1].value:
        if cards1[i].initord > cards1[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable")

print(*[card.suit + card.value for card in cards2])

for i in range(N - 1):
    if cards2[i].value == cards2[i + 1].value:
        if cards2[i].initord > cards2[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable") class Card:
    def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i Card def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i __init__ self.suit = data[0] self.suit self suit data[0] data 0 self.value = data[1] self.value self value data[1] data 1 self.initord = i self.initord self initord i self self data data i i def BubbleSort(cards, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] BubbleSort for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                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].value < cards[j - 1].value:
                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].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] cards[j].value < cards[j - 1].value cards[j].value [j] cards j value cards[j - 1].value [j - 1] cards j - 1 j 1 value 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 cards cards N N def SelectionSort(cards, N):
    for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i] SelectionSort for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i] i range(N) range N min_j = i min_j i for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

         j range(i + 1, N) range i + 1 i 1 N if cards[j].value < cards[min_j].value:
                min_j = j

         cards[j].value < cards[min_j].value cards[j].value [j] cards j value cards[min_j].value [min_j] cards min_j value min_j = j min_j j cards[i], cards[min_j] = cards[min_j], cards[i] cards[i] cards i cards[min_j] cards min_j cards[min_j] cards min_j cards[i] cards i cards cards N N N = int(input()) N int(input()) int input() input Card(data, i) for i, data in enumerate(input().split()) for i, data in enumerate(input().split()) i data enumerate(input().split()) enumerate input().split() ().split () input split for i, data in enumerate(input().split()) Card(data, i) Card data i cards = [Card(data, i) for i, data in enumerate(input().split())] cards [Card(data, i) for i, data in enumerate(input().split())] cards1 = cards.copy() cards1 cards.copy() cards.copy cards copy cards2 = cards.copy() cards2 cards.copy() cards.copy cards copy BubbleSort(cards1, N) BubbleSort cards1 N SelectionSort(cards2, N) SelectionSort cards2 N card.suit + card.value for card in cards1 for card in cards1 card cards1 for card in cards1 card.suit + card.value card.suit card suit card.value card value print(*[card.suit + card.value for card in cards1]) print *[card.suit + card.value for card in cards1] [card.suit + card.value for card in cards1] for i in range(N - 1):
    if cards1[i].value == cards1[i + 1].value:
        if cards1[i].initord > cards1[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable") i range(N - 1) range N - 1 N 1 if cards1[i].value == cards1[i + 1].value:
        if cards1[i].initord > cards1[i + 1].initord:
            print("Not stable")
            break cards1[i].value == cards1[i + 1].value cards1[i].value [i] cards1 i value cards1[i + 1].value [i + 1] cards1 i + 1 i 1 value if cards1[i].initord > cards1[i + 1].initord:
            print("Not stable")
            break cards1[i].initord > cards1[i + 1].initord cards1[i].initord [i] cards1 i initord cards1[i + 1].initord [i + 1] cards1 i + 1 i 1 initord print("Not stable") print "Not stable" break print("Stable") print "Stable" card.suit + card.value for card in cards2 for card in cards2 card cards2 for card in cards2 card.suit + card.value card.suit card suit card.value card value print(*[card.suit + card.value for card in cards2]) print *[card.suit + card.value for card in cards2] [card.suit + card.value for card in cards2] for i in range(N - 1):
    if cards2[i].value == cards2[i + 1].value:
        if cards2[i].initord > cards2[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable") i range(N - 1) range N - 1 N 1 if cards2[i].value == cards2[i + 1].value:
        if cards2[i].initord > cards2[i + 1].initord:
            print("Not stable")
            break cards2[i].value == cards2[i + 1].value cards2[i].value [i] cards2 i value cards2[i + 1].value [i + 1] cards2 i + 1 i 1 value if cards2[i].initord > cards2[i + 1].initord:
            print("Not stable")
            break cards2[i].initord > cards2[i + 1].initord cards2[i].initord [i] cards2 i initord cards2[i + 1].initord [i + 1] cards2 i + 1 i 1 initord print("Not stable") print "Not stable" break print("Stable") print "Stable" def BubbleSort(cards, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] def BubbleSort(cards, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] BubbleSort cards2 = cards.copy() cards.copy() cards2 cards = [Card(data, i) for i, data in enumerate(input().split())] [Card(data, i) for i, data in enumerate(input().split())] cards cards1 = cards.copy() cards.copy() cards1 class Card:
    def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i class Card:
    def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i Card def SelectionSort(cards, N):
    for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i] def SelectionSort(cards, N):
    for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i] SelectionSort N = int(input()) int(input()) N

class Card:
    def __init__(self, data, i):
        self.suit = data[0]
        self.value = data[1]
        self.initord = i

def BubbleSort(cards, N):
    for i in range(N):
        for j in range(N - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j]

def SelectionSort(cards, N):
    for i in range(N):
        min_j = i

        for j in range(i + 1, N):
            if cards[j].value < cards[min_j].value:
                min_j = j

        cards[i], cards[min_j] = cards[min_j], cards[i]

N = int(input())
cards = [Card(data, i) for i, data in enumerate(input().split())]

cards1 = cards.copy()
cards2 = cards.copy()

BubbleSort(cards1, N)
SelectionSort(cards2, N)

print(*[card.suit + card.value for card in cards1])

for i in range(N - 1):
    if cards1[i].value == cards1[i + 1].value:
        if cards1[i].initord > cards1[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable")

print(*[card.suit + card.value for card in cards2])

for i in range(N - 1):
    if cards2[i].value == cards2[i + 1].value:
        if cards2[i].initord > cards2[i + 1].initord:
            print("Not stable")
            break
else:
    print("Stable")