_, cards = input(), input().split()

def bubble_sort(array):
    A = array[:]
    for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
    return A

def selection_sort(array):
    A = array[:]
    for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
    return A
def is_stable(org, sorted):

    def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

    return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable"

a = bubble_sort(cards)
print(" ".join(a))
print(is_stable(cards, a))
b = selection_sort(cards)
print(" ".join(b))
print(is_stable(cards, b)) _, cards = input(), input().split() _ input() input cards input().split() ().split () input split def bubble_sort(array):
    A = array[:]
    for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
    return A bubble_sort A = array[:] A array[:] array : for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
     i range(len(A)) range len(A) len A for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
     j range(1, len(A) - i) range 1 len(A) - i len(A) len A i if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
     int(A[j - 1][1]) > int(A[j][1]) int(A[j - 1][1]) int A[j - 1][1] [j - 1] A j - 1 j 1 1 int(A[j][1]) int A[j][1] [j] A j 1 A[j], A[j - 1] = A[j - 1], A[j] A[j] A j A[j - 1] A j - 1 j 1 A[j - 1] A j - 1 j 1 A[j] A j return A A array array def selection_sort(array):
    A = array[:]
    for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
    return A selection_sort A = array[:] A array[:] array : for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
     i range(len(A)) range len(A) len A minn = i minn i for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
         j range(i, len(A)) range i len(A) len A if int(A[j][1]) < int(A[minn][1]): minn = j
         int(A[j][1]) < int(A[minn][1]) int(A[j][1]) int A[j][1] [j] A j 1 int(A[minn][1]) int A[minn][1] [minn] A minn 1 A[i], A[minn] = A[minn], A[i] A[i] A i A[minn] A minn A[minn] A minn A[i] A i return A A array array def is_stable(org, sorted):

    def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

    return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable" is_stable def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

     get_same_numbers result = {} result {} for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
         i range(1, 10) range 1 10 result[i] = [] result[i] result i [] for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
         j range(len(array)) range len(array) len array if int(array[j][1]) == i:
                    result[i].append(array[j])
         int(array[j][1]) == i int(array[j][1]) int array[j][1] [j] array j 1 i result[i].append(array[j]) [i].append [i] result i append array[j] array j cards for _, cards in result.items() if len(cards) >= 2 for _, cards in result.items() if len(cards) >= 2 _ cards result.items() result.items result items len(cards) >= 2 len(cards) len cards 2 if len(cards) >= 2 cards return [cards for _, cards in result.items() if len(cards) >= 2] [cards for _, cards in result.items() if len(cards) >= 2] array array return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable" "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable" get_same_numbers(org) == get_same_numbers(sorted) get_same_numbers(org) get_same_numbers org get_same_numbers(sorted) get_same_numbers sorted "Stable" "Not stable" org org sorted sorted a = bubble_sort(cards) a bubble_sort(cards) bubble_sort cards print(" ".join(a)) print " ".join(a) " ".join " " join a print(is_stable(cards, a)) print is_stable(cards, a) is_stable cards a b = selection_sort(cards) b selection_sort(cards) selection_sort cards print(" ".join(b)) print " ".join(b) " ".join " " join b print(is_stable(cards, b)) print is_stable(cards, b) is_stable cards b def selection_sort(array):
    A = array[:]
    for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
    return A def selection_sort(array):
    A = array[:]
    for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
    return A selection_sort cards = input(), input().split() input().split() cards def is_stable(org, sorted):

    def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

    return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable" def is_stable(org, sorted):

    def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

    return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable" is_stable _, cards = input(), input().split() input() _ def bubble_sort(array):
    A = array[:]
    for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
    return A def bubble_sort(array):
    A = array[:]
    for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
    return A bubble_sort b = selection_sort(cards) selection_sort(cards) b a = bubble_sort(cards) bubble_sort(cards) a
_, cards = input(), input().split()

def bubble_sort(array):
    A = array[:]
    for i in range(len(A)):
        for j in range(1, len(A) - i):
            if int(A[j - 1][1]) > int(A[j][1]):
                A[j], A[j - 1] = A[j - 1], A[j]
    return A

def selection_sort(array):
    A = array[:]
    for i in range(len(A)):
        minn = i
        for j in range(i, len(A)):
            if int(A[j][1]) < int(A[minn][1]): minn = j
        A[i], A[minn] = A[minn], A[i]
    return A
def is_stable(org, sorted):

    def get_same_numbers(array):
        result = {}
        for i in range(1, 10):
            result[i] = []
            for j in range(len(array)):
                if int(array[j][1]) == i:
                    result[i].append(array[j])
        return [cards for _, cards in result.items() if len(cards) >= 2]

    return "Stable" if get_same_numbers(org) == get_same_numbers(sorted) else "Not stable"

a = bubble_sort(cards)
print(" ".join(a))
print(is_stable(cards, a))
b = selection_sort(cards)
print(" ".join(b))
print(is_stable(cards, b))
