# Stable Sort

length = int(input())
cards = input().rstrip().split()
cards1 = [card for card in cards]
cards2 = [card for card in cards]

def value(n):
    number = n[1:]
    return int(number)

def char(cards):
    charSets = [[] for i in range(10)]
    for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
    return charSets

def bubble(cards):
    for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
    return cards

def selection(cards):
    # count = 0
    for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

def stable(cards1, cards2):
    if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable'

c = cards
b = bubble(cards1)
s = selection(cards2)

print(' '.join(b))
print(stable(c, b))
print(' '.join(s))
print(stable(c, s))
 length = int(input()) length int(input()) int input() input cards = input().rstrip().split() cards input().rstrip().split() ().rstrip().split ().rstrip() ().rstrip () input rstrip split card for card in cards for card in cards card cards for card in cards card cards1 = [card for card in cards] cards1 [card for card in cards] card for card in cards for card in cards card cards for card in cards card cards2 = [card for card in cards] cards2 [card for card in cards] def value(n):
    number = n[1:]
    return int(number) value number = n[1:] number n[1:] n 1: 1 return int(number) int(number) int number n n def char(cards):
    charSets = [[] for i in range(10)]
    for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
    return charSets char [] for i in range(10) for i in range(10) i range(10) range 10 for i in range(10) [] charSets = [[] for i in range(10)] charSets [[] for i in range(10)] for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
     i range(length) range length char = cards[i][:1] char cards[i][:1] [i] cards i :1 1 val = value(cards[i]) val value(cards[i]) value cards[i] cards i charSets[val].append(char) [val].append [val] charSets val append char return charSets charSets cards cards def bubble(cards):
    for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
    return cards bubble for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
     i range(length) range length j = length - 1 j length - 1 length 1 while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
     j > i j i if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
             value(cards[j]) < value(cards[j - 1]) value(cards[j]) value cards[j] cards j value(cards[j - 1]) value cards[j - 1] cards j - 1 j 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 j -= 1 j 1 return cards cards cards cards def selection(cards):
    # count = 0
    for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards selection for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
     i range(length) range length minj = i minj i for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
         j range(i, length) range i length if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
         value(cards[j]) < value(cards[minj]) value(cards[j]) value cards[j] cards j value(cards[minj]) value cards[minj] cards minj 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 stable(cards1, cards2):
    if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable' stable if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable' char(cards1) == char(cards2) char(cards1) char cards1 char(cards2) char cards2 return 'Stable' 'Stable' return 'Not stable' 'Not stable' cards1 cards1 cards2 cards2 c = cards c cards b = bubble(cards1) b bubble(cards1) bubble cards1 s = selection(cards2) s selection(cards2) selection cards2 print(' '.join(b)) print ' '.join(b) ' '.join ' ' join b print(stable(c, b)) print stable(c, b) stable c b print(' '.join(s)) print ' '.join(s) ' '.join ' ' join s print(stable(c, s)) print stable(c, s) stable c s def bubble(cards):
    for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
    return cards def bubble(cards):
    for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
    return cards bubble c = cards cards c def stable(cards1, cards2):
    if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable' def stable(cards1, cards2):
    if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable' stable cards2 = [card for card in cards] [card for card in cards] cards2 def char(cards):
    charSets = [[] for i in range(10)]
    for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
    return charSets def char(cards):
    charSets = [[] for i in range(10)]
    for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
    return charSets char s = selection(cards2) selection(cards2) s length = int(input()) int(input()) length cards = input().rstrip().split() input().rstrip().split() cards def value(n):
    number = n[1:]
    return int(number) def value(n):
    number = n[1:]
    return int(number) value def selection(cards):
    # count = 0
    for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards def selection(cards):
    # count = 0
    for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards selection b = bubble(cards1) bubble(cards1) b cards1 = [card for card in cards] [card for card in cards] cards1
# Stable Sort

length = int(input())
cards = input().rstrip().split()
cards1 = [card for card in cards]
cards2 = [card for card in cards]

def value(n):
    number = n[1:]
    return int(number)

def char(cards):
    charSets = [[] for i in range(10)]
    for i in range(length):
        char = cards[i][:1]
        val = value(cards[i])
        # print('絵柄: ' + char + ', 数字: ' + str(val))
        charSets[val].append(char)
    return charSets

def bubble(cards):
    for i in range(length):
        j = length - 1
        while j > i:
            if value(cards[j]) < value(cards[j - 1]):
                cards[j], cards[j - 1] = cards[j - 1], cards[j]
            j -= 1
    return cards

def selection(cards):
    # count = 0
    for i in range(length):
        minj = i
        for j in range(i, length):
            if value(cards[j]) < value(cards[minj]):
                minj = j
        #if minj != i:
            # count += 1
        cards[i], cards[minj] = cards[minj], cards[i]
    return cards

def stable(cards1, cards2):
    if char(cards1) == char(cards2):
        return 'Stable'
    else:
        return 'Not stable'

c = cards
b = bubble(cards1)
s = selection(cards2)

print(' '.join(b))
print(stable(c, b))
print(' '.join(s))
print(stable(c, s))

