def bubbleSort(cards, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
    return cards

def selectionSort(cards,N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
    return cards

class Cards(object):
    
    def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

    def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

    def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

    def show(self):
        print(' '.join(self.cards))

    def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

    def copy(self):
        return Cards([card for card in self.cards])

def run():
    n = int(input())
    cards = Cards(input().split())
    cards_sorted = bubbleSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

    cards_sorted = selectionSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

if __name__ == '__main__':
    run() def bubbleSort(cards, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
    return cards bubbleSort flag = True flag True while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
     flag flag = False flag False for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
     j range(N-1, 0, -1) range N-1 N 1 0 -1 if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
     cards.numbers[j] < cards.numbers[j - 1] cards.numbers[j] cards.numbers cards numbers j cards.numbers[j - 1] cards.numbers cards numbers j - 1 j 1 cards.swap(j, j-1) cards.swap cards swap j j-1 j 1 flag = True flag True 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.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
    return cards selectionSort for i in range(N):
        minj = i
        for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
     i range(N) range N minj = i minj i for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
         j range(i, N) range i N if cards.numbers[j] < cards.numbers[minj]:
                minj = j
         cards.numbers[j] < cards.numbers[minj] cards.numbers[j] cards.numbers cards numbers j cards.numbers[minj] cards.numbers cards numbers minj minj = j minj j if minj != i:
            cards.swap(i, minj)
     minj != i minj i cards.swap(i, minj) cards.swap cards swap i minj return cards cards cards cards N N class Cards(object):
    
    def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

    def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

    def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

    def show(self):
        print(' '.join(self.cards))

    def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

    def copy(self):
        return Cards([card for card in self.cards]) Cards def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

     __init__ self.cards = cards self.cards self cards cards int(card[1]) for card in cards for card in cards card cards for card in cards int(card[1]) int card[1] card 1 self.numbers = [int(card[1]) for card in cards] self.numbers self numbers [int(card[1]) for card in cards] self self cards cards def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

     filter_by_number card for card in self.cards if card.endswith(str(number)) for card in self.cards if card.endswith(str(number)) card self.cards self cards card.endswith(str(number)) card.endswith card endswith str(number) str number if card.endswith(str(number)) card return [card for card in self.cards if card.endswith(str(number))] [card for card in self.cards if card.endswith(str(number))] self self number number def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

     swap self.cards[i], self.cards[j] = self.cards[j], self.cards[i] self.cards[i] self.cards self cards i self.cards[j] self.cards self cards j self.cards[j] self.cards self cards j self.cards[i] self.cards self cards i int(card[1]) for card in self.cards for card in self.cards card self.cards self cards for card in self.cards int(card[1]) int card[1] card 1 self.numbers = [int(card[1]) for card in self.cards] self.numbers self numbers [int(card[1]) for card in self.cards] self self i i j j def show(self):
        print(' '.join(self.cards))

     show print(' '.join(self.cards)) print ' '.join(self.cards) ' '.join ' ' join self.cards self cards self self def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

     is_stable for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
         n range(1,10) range 1 10 suits_x = self.filter_by_number(str(n)) suits_x self.filter_by_number(str(n)) self.filter_by_number self filter_by_number str(n) str n suits_y = cards.filter_by_number(str(n)) suits_y cards.filter_by_number(str(n)) cards.filter_by_number cards filter_by_number str(n) str n if suits_x != suits_y:
                return False
         suits_x != suits_y suits_x suits_y return False False return True True self self cards cards def copy(self):
        return Cards([card for card in self.cards]) copy card for card in self.cards for card in self.cards card self.cards self cards for card in self.cards card return Cards([card for card in self.cards]) Cards([card for card in self.cards]) Cards [card for card in self.cards] self self object def run():
    n = int(input())
    cards = Cards(input().split())
    cards_sorted = bubbleSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

    cards_sorted = selectionSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable') run n = int(input()) n int(input()) int input() input cards = Cards(input().split()) cards Cards(input().split()) Cards input().split() ().split () input split cards_sorted = bubbleSort(cards.copy(),n) cards_sorted bubbleSort(cards.copy(),n) bubbleSort cards.copy() cards.copy cards copy n cards_sorted.show() cards_sorted.show cards_sorted show if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

     cards.is_stable(cards_sorted) cards.is_stable cards is_stable cards_sorted print('Stable') print 'Stable' print('Not stable') print 'Not stable' cards_sorted = selectionSort(cards.copy(),n) cards_sorted selectionSort(cards.copy(),n) selectionSort cards.copy() cards.copy cards copy n cards_sorted.show() cards_sorted.show cards_sorted show if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable') cards.is_stable(cards_sorted) cards.is_stable cards is_stable cards_sorted print('Stable') print 'Stable' print('Not stable') print 'Not stable' if __name__ == '__main__':
    run() __name__ == '__main__' __name__ '__main__' run() run def run():
    n = int(input())
    cards = Cards(input().split())
    cards_sorted = bubbleSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

    cards_sorted = selectionSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable') def run():
    n = int(input())
    cards = Cards(input().split())
    cards_sorted = bubbleSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

    cards_sorted = selectionSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable') run def selectionSort(cards,N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
    return cards def selectionSort(cards,N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
    return cards selectionSort def bubbleSort(cards, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
    return cards def bubbleSort(cards, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
    return cards bubbleSort class Cards(object):
    
    def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

    def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

    def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

    def show(self):
        print(' '.join(self.cards))

    def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

    def copy(self):
        return Cards([card for card in self.cards]) class Cards(object):
    
    def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

    def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

    def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

    def show(self):
        print(' '.join(self.cards))

    def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

    def copy(self):
        return Cards([card for card in self.cards]) Cards
def bubbleSort(cards, N):
    flag = True
    while flag:
        flag = False
        for j in range(N-1, 0, -1):
            if cards.numbers[j] < cards.numbers[j - 1]:
                cards.swap(j, j-1)
                flag = True
    return cards

def selectionSort(cards,N):
    for i in range(N):
        minj = i
        for j in range(i, N):
            if cards.numbers[j] < cards.numbers[minj]:
                minj = j
        if minj != i:
            cards.swap(i, minj)
    return cards

class Cards(object):
    
    def __init__(self, cards):
        self.cards = cards
        self.numbers = [int(card[1]) for card in cards]

    def filter_by_number(self, number):
        return [card for card in self.cards if card.endswith(str(number))]

    def swap(self, i, j):
        self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
        self.numbers = [int(card[1]) for card in self.cards]

    def show(self):
        print(' '.join(self.cards))

    def is_stable(self, cards):
        for n in range(1,10):
            suits_x = self.filter_by_number(str(n))
            suits_y = cards.filter_by_number(str(n))
            if suits_x != suits_y:
                return False
        return True

    def copy(self):
        return Cards([card for card in self.cards])

def run():
    n = int(input())
    cards = Cards(input().split())
    cards_sorted = bubbleSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

    cards_sorted = selectionSort(cards.copy(),n)
    cards_sorted.show()
    if cards.is_stable(cards_sorted):
        print('Stable')
    else:
        print('Not stable')

if __name__ == '__main__':
    run()
