class Card(object):
    def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

    def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

    def getValue(self):
        return self._value

    def toString(self):
        return self._suit + str(self._value)


def card_bubbleSort(lst, n):
    for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

    return lst


def card_selectionSort(lst, n):
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

    return lst


def isStable(in_lst, out_lst, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
    return True


if __name__ == "__main__":
    n = int(input())
    lst = input().split()
    func_table = [card_bubbleSort, card_selectionSort]
    for func in func_table:
        cards = [Card(item) for item in lst]
        result = func(cards, n)
        cards = [Card(item) for item in lst]
        print(" ".join([card.toString() for card in result]))
        if isStable(cards, result, n):
            print("Stable")
        else:
            print("Not stable") class Card(object):
    def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

    def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

    def getValue(self):
        return self._value

    def toString(self):
        return self._suit + str(self._value) Card def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

     __init__ self._suit = card[0] self._suit self _suit card[0] card 0 self._value = int(card[1]) self._value self _value int(card[1]) int card[1] card 1 self self card card def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

     __eq__ if other is None:
            return False
         other is None other None return False False return self.__dict__ == other.__dict__ self.__dict__ == other.__dict__ self.__dict__ self __dict__ other.__dict__ other __dict__ self self other other def getValue(self):
        return self._value

     getValue return self._value self._value self _value self self def toString(self):
        return self._suit + str(self._value) toString return self._suit + str(self._value) self._suit + str(self._value) self._suit self _suit str(self._value) str self._value self _value self self object def card_bubbleSort(lst, n):
    for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

    return lst card_bubbleSort for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

     i range(0, n) range 0 n for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

     j range(n-1, i, -1) range n-1 n 1 i -1 if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

     lst[j].getValue() < lst[j-1].getValue() lst[j].getValue() [j].getValue [j] lst j getValue lst[j-1].getValue() [j-1].getValue [j-1] lst j-1 j 1 getValue tmp = lst[j] tmp lst[j] lst j lst[j] = lst[j-1] lst[j] lst j lst[j-1] lst j-1 j 1 lst[j-1] = tmp lst[j-1] lst j-1 j 1 tmp return lst lst lst lst n n def card_selectionSort(lst, n):
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

    return lst card_selectionSort for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

     i range(0, n) range 0 n minj = i minj i for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

         j range(i, n) range i n if lst[j].getValue() < lst[minj].getValue():
                minj = j

         lst[j].getValue() < lst[minj].getValue() lst[j].getValue() [j].getValue [j] lst j getValue lst[minj].getValue() [minj].getValue [minj] lst minj getValue minj = j minj j if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

     i != minj i minj tmp = lst[i] tmp lst[i] lst i lst[i] = lst[minj] lst[i] lst i lst[minj] lst minj lst[minj] = tmp lst[minj] lst minj tmp return lst lst lst lst n n def isStable(in_lst, out_lst, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
    return True isStable for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
     i range(0, n) range 0 n for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
     j range(i+1, n) range i+1 i 1 n for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
     a range(0, n) range 0 n for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
     b range(a+1, n) range a+1 a 1 n if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
     in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a] in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] in_lst[i].getValue() == in_lst[j].getValue() in_lst[i].getValue() [i].getValue [i] in_lst i getValue in_lst[j].getValue() [j].getValue [j] in_lst j getValue in_lst[i] == out_lst[b] in_lst[i] in_lst i out_lst[b] out_lst b in_lst[j] == out_lst[a] in_lst[j] in_lst j out_lst[a] out_lst a return False False return True True in_lst in_lst out_lst out_lst n n if __name__ == "__main__":
    n = int(input())
    lst = input().split()
    func_table = [card_bubbleSort, card_selectionSort]
    for func in func_table:
        cards = [Card(item) for item in lst]
        result = func(cards, n)
        cards = [Card(item) for item in lst]
        print(" ".join([card.toString() for card in result]))
        if isStable(cards, result, n):
            print("Stable")
        else:
            print("Not stable") __name__ == "__main__" __name__ "__main__" n = int(input()) n int(input()) int input() input lst = input().split() lst input().split() ().split () input split func_table = [card_bubbleSort, card_selectionSort] func_table [card_bubbleSort, card_selectionSort] card_bubbleSort card_selectionSort for func in func_table:
        cards = [Card(item) for item in lst]
        result = func(cards, n)
        cards = [Card(item) for item in lst]
        print(" ".join([card.toString() for card in result]))
        if isStable(cards, result, n):
            print("Stable")
        else:
            print("Not stable") func func_table Card(item) for item in lst for item in lst item lst for item in lst Card(item) Card item cards = [Card(item) for item in lst] cards [Card(item) for item in lst] result = func(cards, n) result func(cards, n) func cards n Card(item) for item in lst for item in lst item lst for item in lst Card(item) Card item cards = [Card(item) for item in lst] cards [Card(item) for item in lst] card.toString() for card in result for card in result card result for card in result card.toString() card.toString card toString print(" ".join([card.toString() for card in result])) print " ".join([card.toString() for card in result]) " ".join " " join [card.toString() for card in result] if isStable(cards, result, n):
            print("Stable")
        else:
            print("Not stable") isStable(cards, result, n) isStable cards result n print("Stable") print "Stable" print("Not stable") print "Not stable" def isStable(in_lst, out_lst, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
    return True def isStable(in_lst, out_lst, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
    return True isStable def card_selectionSort(lst, n):
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

    return lst def card_selectionSort(lst, n):
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

    return lst card_selectionSort cards = [Card(item) for item in lst] [Card(item) for item in lst] cards n = int(input()) int(input()) n func_table = [card_bubbleSort, card_selectionSort] [card_bubbleSort, card_selectionSort] func_table result = func(cards, n) func(cards, n) result def card_bubbleSort(lst, n):
    for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

    return lst def card_bubbleSort(lst, n):
    for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

    return lst card_bubbleSort cards = [Card(item) for item in lst] [Card(item) for item in lst] cards class Card(object):
    def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

    def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

    def getValue(self):
        return self._value

    def toString(self):
        return self._suit + str(self._value) class Card(object):
    def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

    def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

    def getValue(self):
        return self._value

    def toString(self):
        return self._suit + str(self._value) Card lst = input().split() input().split() lst
class Card(object):
    def __init__(self, card):
        self._suit = card[0]
        self._value = int(card[1])

    def __eq__(self, other):
        if other is None:
            return False
        return self.__dict__ == other.__dict__

    def getValue(self):
        return self._value

    def toString(self):
        return self._suit + str(self._value)


def card_bubbleSort(lst, n):
    for i in range(0, n):
        for j in range(n-1, i, -1):
            if lst[j].getValue() < lst[j-1].getValue():
                tmp = lst[j]
                lst[j] = lst[j-1]
                lst[j-1] = tmp

    return lst


def card_selectionSort(lst, n):
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j].getValue() < lst[minj].getValue():
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp

    return lst


def isStable(in_lst, out_lst, n):
    for i in range(0, n):
        for j in range(i+1, n):
            for a in range(0, n):
                for b in range(a+1, n):
                    if in_lst[i].getValue() == in_lst[j].getValue() \
                            and in_lst[i] == out_lst[b] \
                            and in_lst[j] == out_lst[a]:
                                return False
    return True


if __name__ == "__main__":
    n = int(input())
    lst = input().split()
    func_table = [card_bubbleSort, card_selectionSort]
    for func in func_table:
        cards = [Card(item) for item in lst]
        result = func(cards, n)
        cards = [Card(item) for item in lst]
        print(" ".join([card.toString() for card in result]))
        if isStable(cards, result, n):
            print("Stable")
        else:
            print("Not stable")