from typing import List, NamedTuple


class Card(NamedTuple):
    suit: str
    value: int


def bubble_sort(cards: List[Card]):
    for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j]


def selection_sort(cards: List[Card]):
    for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]


def gen_numbet_card_pair(cards: List[Card]):
    return {n: [c for c in cards if c.value == n] for n in range(1, 10)}


def is_stable(in_cards: List[Card], out_cards: List[Card]):
    in_lists = gen_numbet_card_pair(in_cards)
    out_lists = gen_numbet_card_pair(out_cards)
    for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
    return True


def printing(arr, f):
    b_arr = arr.copy()
    f(b_arr)
    print(" ".join([c.suit+str(c.value) for c in b_arr]))
    print("Stable" if is_stable(arr, b_arr) else "Not stable")


n = int(input())
cards: List[Card] = [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")]
printing(cards, bubble_sort)
printing(cards, selection_sort)
 from typing import List, NamedTuple class Card(NamedTuple):
    suit: str
    value: int Card suit: str suit value: int value NamedTuple def bubble_sort(cards: List[Card]):
    for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] bubble_sort for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] i range(len(cards)) range len(cards) len cards for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] j range(len(cards) - 1, i, -1) range len(cards) - 1 len(cards) len cards 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: List[Card] cards def selection_sort(cards: List[Card]):
    for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i] selection_sort for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i] i range(len(cards)) range len(cards) len cards minj = i minj i for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
         j range(i, len(cards)) range i len(cards) len cards if cards[j].value < cards[minj].value:
                minj = j
         cards[j].value < cards[minj].value cards[j].value [j] cards j value cards[minj].value [minj] cards minj value 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 cards: List[Card] cards def gen_numbet_card_pair(cards: List[Card]):
    return {n: [c for c in cards if c.value == n] for n in range(1, 10)} gen_numbet_card_pair n: [c for c in cards if c.value == n] for n in range(1, 10) for n in range(1, 10) n range(1, 10) range 1 10 for n in range(1, 10) n [c for c in cards if c.value == n] return {n: [c for c in cards if c.value == n] for n in range(1, 10)} {n: [c for c in cards if c.value == n] for n in range(1, 10)} cards: List[Card] cards def is_stable(in_cards: List[Card], out_cards: List[Card]):
    in_lists = gen_numbet_card_pair(in_cards)
    out_lists = gen_numbet_card_pair(out_cards)
    for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
    return True is_stable in_lists = gen_numbet_card_pair(in_cards) in_lists gen_numbet_card_pair(in_cards) gen_numbet_card_pair in_cards out_lists = gen_numbet_card_pair(out_cards) out_lists gen_numbet_card_pair(out_cards) gen_numbet_card_pair out_cards for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
     n range(10) range 10 if in_lists.get(n, []) != out_lists.get(n, []):
            return False
     in_lists.get(n, []) != out_lists.get(n, []) in_lists.get(n, []) in_lists.get in_lists get n [] out_lists.get(n, []) out_lists.get out_lists get n [] return False False return True True in_cards: List[Card] in_cards out_cards: List[Card] out_cards def printing(arr, f):
    b_arr = arr.copy()
    f(b_arr)
    print(" ".join([c.suit+str(c.value) for c in b_arr]))
    print("Stable" if is_stable(arr, b_arr) else "Not stable") printing b_arr = arr.copy() b_arr arr.copy() arr.copy arr copy f(b_arr) f b_arr c.suit+str(c.value) for c in b_arr for c in b_arr c b_arr for c in b_arr c.suit+str(c.value) c.suit c suit str(c.value) str c.value c value print(" ".join([c.suit+str(c.value) for c in b_arr])) print " ".join([c.suit+str(c.value) for c in b_arr]) " ".join " " join [c.suit+str(c.value) for c in b_arr] print("Stable" if is_stable(arr, b_arr) else "Not stable") print "Stable" if is_stable(arr, b_arr) else "Not stable" is_stable(arr, b_arr) is_stable arr b_arr "Stable" "Not stable" arr arr f f n = int(input()) n int(input()) int input() input Card(suit=x[0], value=int(x[1])) for x in input().split(" ") for x in input().split(" ") x input().split(" ") ().split () input split " " for x in input().split(" ") Card(suit=x[0], value=int(x[1])) Card suit=x[0] suit x[0] x 0 value=int(x[1]) value int(x[1]) int x[1] x 1 cards: List[Card] = [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")] cards [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")] printing(cards, bubble_sort) printing cards bubble_sort printing(cards, selection_sort) printing cards selection_sort cards: List[Card] = [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")] [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")] cards def is_stable(in_cards: List[Card], out_cards: List[Card]):
    in_lists = gen_numbet_card_pair(in_cards)
    out_lists = gen_numbet_card_pair(out_cards)
    for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
    return True def is_stable(in_cards: List[Card], out_cards: List[Card]):
    in_lists = gen_numbet_card_pair(in_cards)
    out_lists = gen_numbet_card_pair(out_cards)
    for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
    return True is_stable def gen_numbet_card_pair(cards: List[Card]):
    return {n: [c for c in cards if c.value == n] for n in range(1, 10)} def gen_numbet_card_pair(cards: List[Card]):
    return {n: [c for c in cards if c.value == n] for n in range(1, 10)} gen_numbet_card_pair def selection_sort(cards: List[Card]):
    for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i] def selection_sort(cards: List[Card]):
    for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i] selection_sort class Card(NamedTuple):
    suit: str
    value: int class Card(NamedTuple):
    suit: str
    value: int Card n = int(input()) int(input()) n def bubble_sort(cards: List[Card]):
    for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] def bubble_sort(cards: List[Card]):
    for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j] bubble_sort def printing(arr, f):
    b_arr = arr.copy()
    f(b_arr)
    print(" ".join([c.suit+str(c.value) for c in b_arr]))
    print("Stable" if is_stable(arr, b_arr) else "Not stable") def printing(arr, f):
    b_arr = arr.copy()
    f(b_arr)
    print(" ".join([c.suit+str(c.value) for c in b_arr]))
    print("Stable" if is_stable(arr, b_arr) else "Not stable") printing
from typing import List, NamedTuple


class Card(NamedTuple):
    suit: str
    value: int


def bubble_sort(cards: List[Card]):
    for i in range(len(cards)):
        for j in range(len(cards) - 1, i, -1):
            if cards[j].value < cards[j - 1].value:
                cards[j], cards[j - 1] = cards[j - 1], cards[j]


def selection_sort(cards: List[Card]):
    for i in range(len(cards)):
        minj = i
        for j in range(i, len(cards)):
            if cards[j].value < cards[minj].value:
                minj = j
        cards[i], cards[minj] = cards[minj], cards[i]


def gen_numbet_card_pair(cards: List[Card]):
    return {n: [c for c in cards if c.value == n] for n in range(1, 10)}


def is_stable(in_cards: List[Card], out_cards: List[Card]):
    in_lists = gen_numbet_card_pair(in_cards)
    out_lists = gen_numbet_card_pair(out_cards)
    for n in range(10):
        if in_lists.get(n, []) != out_lists.get(n, []):
            return False
    return True


def printing(arr, f):
    b_arr = arr.copy()
    f(b_arr)
    print(" ".join([c.suit+str(c.value) for c in b_arr]))
    print("Stable" if is_stable(arr, b_arr) else "Not stable")


n = int(input())
cards: List[Card] = [Card(suit=x[0], value=int(x[1])) for x in input().split(" ")]
printing(cards, bubble_sort)
printing(cards, selection_sort)

