class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
    
def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i]

def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j]

def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable")

def printcards(cards):
    for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='')

def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
    
if __name__ == '__main__':
    main() class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
     Card def __init__(self, suit, value):
        self.suit = suit
        self.value = value
     __init__ self.suit = suit self.suit self suit suit self.value = value self.value self value value self self suit suit value value def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i] selection for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i] i range(len(ls)) range len(ls) len ls minj = i minj i for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
         j range(i, len(ls)) range i len(ls) len ls if ls[j].value < ls[minj].value:
                minj = j
         ls[j].value < ls[minj].value ls[j].value [j] ls j value ls[minj].value [minj] ls minj value minj = j minj j ls[i], ls[minj] = ls[minj], ls[i] ls[i] ls i ls[minj] ls minj ls[minj] ls minj ls[i] ls i ls ls def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] bubblesort for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] i range(1, len(ls)) range 1 len(ls) len ls for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] j reversed(range(i, len(ls))) reversed range(i, len(ls)) range i len(ls) len ls if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] ls[j].value < ls[j-1].value ls[j].value [j] ls j value ls[j-1].value [j-1] ls j-1 j 1 value ls[j], ls[j-1] = ls[j-1], ls[j] ls[j] ls j ls[j-1] ls j-1 j 1 ls[j-1] ls j-1 j 1 ls[j] ls j ls ls def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable") isStable if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable") ls1 == ls2 ls1 ls2 print("Stable") print "Stable" print("Not stable") print "Not stable" ls1 ls1 ls2 ls2 def printcards(cards):
    for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='') printcards for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='') x range(len(cards)) range len(cards) len cards if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='') x == len(cards) - 1 x len(cards) - 1 len(cards) len cards 1 print(cards[x].suit + cards[x].value) print cards[x].suit + cards[x].value cards[x].suit [x] cards x suit cards[x].value [x] cards x value print(cards[x].suit + cards[x].value, ' ', sep='', end='') print cards[x].suit + cards[x].value cards[x].suit [x] cards x suit cards[x].value [x] cards x value ' ' sep='' sep '' end='' end '' cards cards def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
     main N = int(input().rstrip()) N int(input().rstrip()) int input().rstrip() ().rstrip () input rstrip cards = [] cards [] for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

     card list(input().split(' ')) list input().split(' ') ().split () input split ' ' cards.append(Card(card[0], card[1])) cards.append cards append Card(card[0], card[1]) Card card[0] card 0 card[1] card 1 x for x in cards for x in cards x cards for x in cards x a = [x for x in cards] a [x for x in cards] x for x in cards for x in cards x cards for x in cards x b = [x for x in cards] b [x for x in cards] bubblesort(a) bubblesort a selection(b) selection b printcards(a) printcards a print("Stable") print "Stable" printcards(b) printcards b isStable(a,b) isStable a b if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
     class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
     Card def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable") def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable") isStable def printcards(cards):
    for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='') def printcards(cards):
    for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='') printcards def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i] def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i] selection def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j] bubblesort def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
     def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
     main
class Card:
    def __init__(self, suit, value):
        self.suit = suit
        self.value = value
    
def selection(ls):
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j].value < ls[minj].value:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i]

def bubblesort(ls):
    for i in range(1, len(ls)):
        for j in reversed(range(i, len(ls))):
            if ls[j].value < ls[j-1].value:
                ls[j], ls[j-1] = ls[j-1], ls[j]

def isStable(ls1, ls2):
    if ls1 == ls2:
        print("Stable")
    else:
        print("Not stable")

def printcards(cards):
    for x in range(len(cards)):
        if x == len(cards) - 1:
            print(cards[x].suit + cards[x].value)
        else:
            print(cards[x].suit + cards[x].value, ' ', sep='', end='')

def main():
    N = int(input().rstrip())

    cards = []
    for card in list(input().split(' ')):
        cards.append(Card(card[0], card[1]))

    a = [x for x in cards]
    b = [x for x in cards]
    

    bubblesort(a)
    selection(b)
    
    printcards(a)
    print("Stable")

    printcards(b)
    isStable(a,b)
    
if __name__ == '__main__':
    main()