class Card:
    def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

    def __lt__(self, another_card):
        return self.value < another_card.value

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


def bubble_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
    return A


def selection_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
    return A


N = int(input())
A = [Card(s) for s in input().split()]
A_sorted_by_bsort = bubble_sort(A)
A_sorted_by_ssort = selection_sort(A)
print(*A_sorted_by_bsort)
print("Stable")
print(*A_sorted_by_ssort)
print("Stable" if A_sorted_by_ssort == A_sorted_by_bsort else "Not stable")
 class Card:
    def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

    def __lt__(self, another_card):
        return self.value < another_card.value

    def __repr__(self):
        return self.suit + str(self.value) Card def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

     __init__ self.suit = s[0] self.suit self suit s[0] s 0 self.value = int(s[1]) self.value self value int(s[1]) int s[1] s 1 self self s s def __lt__(self, another_card):
        return self.value < another_card.value

     __lt__ return self.value < another_card.value self.value < another_card.value self.value self value another_card.value another_card value self self another_card another_card def __repr__(self):
        return self.suit + str(self.value) __repr__ 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(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
    return A bubble_sort n = len(A) n len(A) len A A = A[:] A A[:] A : for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
     i range(n) range n for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
     j reversed(range(i + 1, n)) reversed range(i + 1, n) range i + 1 i 1 n if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
     A[j] < A[j - 1] A[j] A j A[j - 1] A j - 1 j 1 A[j - 1], A[j] = A[j], A[j - 1] A[j - 1] A j - 1 j 1 A[j] A j A[j] A j A[j - 1] A j - 1 j 1 return A A A A def selection_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
    return A selection_sort n = len(A) n len(A) len A A = A[:] A A[:] A : for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
     i range(n) range n min_idx = i min_idx i for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
         j range(i + 1, n) range i + 1 i 1 n if A[j] < A[min_idx]:
                min_idx = j
         A[j] < A[min_idx] A[j] A j A[min_idx] A min_idx min_idx = j min_idx j A[min_idx], A[i] = A[i], A[min_idx] A[min_idx] A min_idx A[i] A i A[i] A i A[min_idx] A min_idx return A A A A N = int(input()) N int(input()) int input() input Card(s) for s in input().split() for s in input().split() s input().split() ().split () input split for s in input().split() Card(s) Card s A = [Card(s) for s in input().split()] A [Card(s) for s in input().split()] A_sorted_by_bsort = bubble_sort(A) A_sorted_by_bsort bubble_sort(A) bubble_sort A A_sorted_by_ssort = selection_sort(A) A_sorted_by_ssort selection_sort(A) selection_sort A print(*A_sorted_by_bsort) print *A_sorted_by_bsort A_sorted_by_bsort print("Stable") print "Stable" print(*A_sorted_by_ssort) print *A_sorted_by_ssort A_sorted_by_ssort print("Stable" if A_sorted_by_ssort == A_sorted_by_bsort else "Not stable") print "Stable" if A_sorted_by_ssort == A_sorted_by_bsort else "Not stable" A_sorted_by_ssort == A_sorted_by_bsort A_sorted_by_ssort A_sorted_by_bsort "Stable" "Not stable" class Card:
    def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

    def __lt__(self, another_card):
        return self.value < another_card.value

    def __repr__(self):
        return self.suit + str(self.value) class Card:
    def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

    def __lt__(self, another_card):
        return self.value < another_card.value

    def __repr__(self):
        return self.suit + str(self.value) Card A_sorted_by_bsort = bubble_sort(A) bubble_sort(A) A_sorted_by_bsort A = [Card(s) for s in input().split()] [Card(s) for s in input().split()] A def selection_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
    return A def selection_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
    return A selection_sort N = int(input()) int(input()) N def bubble_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
    return A def bubble_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
    return A bubble_sort A_sorted_by_ssort = selection_sort(A) selection_sort(A) A_sorted_by_ssort
class Card:
    def __init__(self, s):
        self.suit = s[0]
        self.value = int(s[1])

    def __lt__(self, another_card):
        return self.value < another_card.value

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


def bubble_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        for j in reversed(range(i + 1, n)):
            if A[j] < A[j - 1]:
                A[j - 1], A[j] = A[j], A[j - 1]
    return A


def selection_sort(A):
    n = len(A)
    A = A[:]
    for i in range(n):
        min_idx = i
        for j in range(i + 1, n):
            if A[j] < A[min_idx]:
                min_idx = j
        A[min_idx], A[i] = A[i], A[min_idx]
    return A


N = int(input())
A = [Card(s) for s in input().split()]
A_sorted_by_bsort = bubble_sort(A)
A_sorted_by_ssort = selection_sort(A)
print(*A_sorted_by_bsort)
print("Stable")
print(*A_sorted_by_ssort)
print("Stable" if A_sorted_by_ssort == A_sorted_by_bsort else "Not stable")

