class Card:
    def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

    def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

    def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

    def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

    def __hash__(self):
        return hash((self.suit, self.value))

    def __str__(self):
        return self.suit + str(self.value)


def bubble_sort(deck):
    """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """
    size = len(deck)
    for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

    return deck


def selection_sort(deck):
    """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """
    size = len(deck)
    for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

    return deck


def is_stable_sorted(org, deck):
    """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """
    idx = {c:i for (i, c) in enumerate(org)}
    return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])])


def check_stable(org, deck):
    if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable"


def run():
    _ = int(input()) # flake8: noqa
    deck = []
    for lb in input().split():
        deck.append(Card(lb))

    db = bubble_sort(deck[:])
    print(" ".join([str(c) for c in db]))
    print(check_stable(deck, db))

    ds = selection_sort(deck[:])
    print(" ".join([str(c) for c in ds]))
    print(check_stable(deck, ds))


if __name__ == '__main__':
    run()
 class Card:
    def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

    def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

    def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

    def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

    def __hash__(self):
        return hash((self.suit, self.value))

    def __str__(self):
        return self.suit + str(self.value) Card def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

     __init__ suits = ['S', 'H', 'C', 'D'] suits ['S', 'H', 'C', 'D'] 'S' 'H' 'C' 'D' suit, value = list(label) suit list(label) list label value assert(suit in suits and 0 < int(value) < 10) suit in suits and 0 < int(value) < 10 suit in suits suit suits 0 < int(value) < 10 0 < int(value) 0 int(value) int value 10 self.suit = suit self.suit self suit suit self.value = int(value) self.value self value int(value) int value self self label label def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

     __eq__ """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """ return self.value == other.value self.value == other.value self.value self value other.value other value self self other other def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

     __lt__ """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """ return self.value < other.value self.value < other.value self.value self value other.value other value self self other other def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

     __gt__ """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """ return self.value > other.value self.value > other.value self.value self value other.value other value self self other other def __hash__(self):
        return hash((self.suit, self.value))

     __hash__ return hash((self.suit, self.value)) hash((self.suit, self.value)) hash (self.suit, self.value) self.suit self suit self.value self value self self def __str__(self):
        return self.suit + str(self.value) __str__ 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 def bubble_sort(deck):
    """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """
    size = len(deck)
    for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

    return deck bubble_sort """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """ size = len(deck) size len(deck) len deck for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

     i range(size) range size for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

     j reversed(range(i+1, size)) reversed range(i+1, size) range i+1 i 1 size if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

     deck[j] < deck[j-1] deck[j] deck j deck[j-1] deck j-1 j 1 deck[j-1], deck[j] = deck[j], deck[j-1] deck[j-1] deck j-1 j 1 deck[j] deck j deck[j] deck j deck[j-1] deck j-1 j 1 return deck deck deck deck def selection_sort(deck):
    """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """
    size = len(deck)
    for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

    return deck selection_sort """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """ size = len(deck) size len(deck) len deck for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

     i range(size) range size mini = i mini i for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
         j range(i, size) range i size if deck[mini] > deck[j]:
                mini = j
         deck[mini] > deck[j] deck[mini] deck mini deck[j] deck j mini = j mini j deck[i], deck[mini] = deck[mini], deck[i] deck[i] deck i deck[mini] deck mini deck[mini] deck mini deck[i] deck i return deck deck deck deck def is_stable_sorted(org, deck):
    """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """
    idx = {c:i for (i, c) in enumerate(org)}
    return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])]) is_stable_sorted """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """ c:i for (i, c) in enumerate(org) for (i, c) in enumerate(org) (i, c) i c enumerate(org) enumerate org for (i, c) in enumerate(org) c i idx = {c:i for (i, c) in enumerate(org)} idx {c:i for (i, c) in enumerate(org)} c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:]) for (c1, c2) in zip(deck[:-1], deck[1:]) (c1, c2) c1 c2 zip(deck[:-1], deck[1:]) zip deck[:-1] deck :-1 -1 deck[1:] deck 1: 1 for (c1, c2) in zip(deck[:-1], deck[1:]) c1 < c2 or idx[c1] < idx[c2] c1 < c2 c1 c2 idx[c1] < idx[c2] idx[c1] idx c1 idx[c2] idx c2 return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])]) all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])]) all [c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])] org org deck deck def check_stable(org, deck):
    if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable" check_stable if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable" is_stable_sorted(org, deck) is_stable_sorted org deck return "Stable" "Stable" return "Not stable" "Not stable" org org deck deck def run():
    _ = int(input()) # flake8: noqa
    deck = []
    for lb in input().split():
        deck.append(Card(lb))

    db = bubble_sort(deck[:])
    print(" ".join([str(c) for c in db]))
    print(check_stable(deck, db))

    ds = selection_sort(deck[:])
    print(" ".join([str(c) for c in ds]))
    print(check_stable(deck, ds)) run _ = int(input()) _ int(input()) int input() input deck = [] deck [] for lb in input().split():
        deck.append(Card(lb))

     lb input().split() ().split () input split deck.append(Card(lb)) deck.append deck append Card(lb) Card lb db = bubble_sort(deck[:]) db bubble_sort(deck[:]) bubble_sort deck[:] deck : str(c) for c in db for c in db c db for c in db str(c) str c print(" ".join([str(c) for c in db])) print " ".join([str(c) for c in db]) " ".join " " join [str(c) for c in db] print(check_stable(deck, db)) print check_stable(deck, db) check_stable deck db ds = selection_sort(deck[:]) ds selection_sort(deck[:]) selection_sort deck[:] deck : str(c) for c in ds for c in ds c ds for c in ds str(c) str c print(" ".join([str(c) for c in ds])) print " ".join([str(c) for c in ds]) " ".join " " join [str(c) for c in ds] print(check_stable(deck, ds)) print check_stable(deck, ds) check_stable deck ds if __name__ == '__main__':
    run() __name__ == '__main__' __name__ '__main__' run() run class Card:
    def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

    def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

    def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

    def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

    def __hash__(self):
        return hash((self.suit, self.value))

    def __str__(self):
        return self.suit + str(self.value) class Card:
    def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

    def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

    def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

    def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

    def __hash__(self):
        return hash((self.suit, self.value))

    def __str__(self):
        return self.suit + str(self.value) Card def selection_sort(deck):
    """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """
    size = len(deck)
    for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

    return deck def selection_sort(deck):
    """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """
    size = len(deck)
    for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

    return deck selection_sort def check_stable(org, deck):
    if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable" def check_stable(org, deck):
    if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable" check_stable def run():
    _ = int(input()) # flake8: noqa
    deck = []
    for lb in input().split():
        deck.append(Card(lb))

    db = bubble_sort(deck[:])
    print(" ".join([str(c) for c in db]))
    print(check_stable(deck, db))

    ds = selection_sort(deck[:])
    print(" ".join([str(c) for c in ds]))
    print(check_stable(deck, ds)) def run():
    _ = int(input()) # flake8: noqa
    deck = []
    for lb in input().split():
        deck.append(Card(lb))

    db = bubble_sort(deck[:])
    print(" ".join([str(c) for c in db]))
    print(check_stable(deck, db))

    ds = selection_sort(deck[:])
    print(" ".join([str(c) for c in ds]))
    print(check_stable(deck, ds)) run def is_stable_sorted(org, deck):
    """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """
    idx = {c:i for (i, c) in enumerate(org)}
    return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])]) def is_stable_sorted(org, deck):
    """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """
    idx = {c:i for (i, c) in enumerate(org)}
    return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])]) is_stable_sorted def bubble_sort(deck):
    """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """
    size = len(deck)
    for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

    return deck def bubble_sort(deck):
    """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """
    size = len(deck)
    for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

    return deck bubble_sort
class Card:
    def __init__(self, label):
        suits = ['S', 'H', 'C', 'D']
        suit, value = list(label)
        assert(suit in suits and 0 < int(value) < 10)

        self.suit = suit
        self.value = int(value)

    def __eq__(self, other):
        """equal

        >>> Card('S1') == Card('D9')
        False
        >>> Card('D9') == Card('S1')
        False
        >>> Card('C5') == Card('C5')
        True
        """
        return self.value == other.value

    def __lt__(self, other):
        """less than

        >>> Card('S1') < Card('D9')
        True
        >>> Card('D9') < Card('S1')
        False
        """
        return self.value < other.value

    def __gt__(self, other):
        """greater than

        >>> Card('S1') > Card('D9')
        False
        >>> Card('D9') > Card('S1')
        True
        """
        return self.value > other.value

    def __hash__(self):
        return hash((self.suit, self.value))

    def __str__(self):
        return self.suit + str(self.value)


def bubble_sort(deck):
    """sort deck by bubble sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in bubble_sort(deck)]))
    D2 C3 H4 S4 C9
    """
    size = len(deck)
    for i in range(size):
        for j in reversed(range(i+1, size)):
            if deck[j] < deck[j-1]:
                deck[j-1], deck[j] = deck[j], deck[j-1]

    return deck


def selection_sort(deck):
    """sort deck by selection sort

    >>> deck = [Card('H4'), Card('C9'), Card('S4'), Card('D2'), Card('C3')]
    >>> print(" ".join([str(c) for c in selection_sort(deck)]))
    D2 C3 S4 H4 C9
    """
    size = len(deck)
    for i in range(size):
        mini = i
        for j in range(i, size):
            if deck[mini] > deck[j]:
                mini = j
        deck[i], deck[mini] = deck[mini], deck[i]

    return deck


def is_stable_sorted(org, deck):
    """check if deck is sorted by stable manner

    >>> c1 = Card('S1')
    >>> c2 = Card('D1')
    >>> is_stable_sorted([c1, c2], [c1, c2])
    True
    >>> is_stable_sorted([c1, c2], [c2, c1])
    False
    """
    idx = {c:i for (i, c) in enumerate(org)}
    return all([c1 < c2 or idx[c1] < idx[c2]
                for (c1, c2) in zip(deck[:-1], deck[1:])])


def check_stable(org, deck):
    if is_stable_sorted(org, deck):
        return "Stable"
    else:
        return "Not stable"


def run():
    _ = int(input()) # flake8: noqa
    deck = []
    for lb in input().split():
        deck.append(Card(lb))

    db = bubble_sort(deck[:])
    print(" ".join([str(c) for c in db]))
    print(check_stable(deck, db))

    ds = selection_sort(deck[:])
    print(" ".join([str(c) for c in ds]))
    print(check_stable(deck, ds))


if __name__ == '__main__':
    run()

