from collections import namedtuple
C = namedtuple("C", "m v")


def bubble_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
    return A, True


def selection_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
    return A


lst = ["Not stable", "Stable"]
n = int(input())
A = [C(x[0], x[1]) for x in input().split()]

B, j = bubble_sort(A)
print(*[(B[i].m + B[i].v) for i in range(n)])
print(lst[j])

S = selection_sort(A)
print(*[(S[i].m + S[i].v) for i in range(n)])
print(lst[B == S])
 from collections import namedtuple C = namedtuple("C", "m v") C namedtuple("C", "m v") namedtuple "C" "m v" def bubble_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
    return A, True bubble_sort A = A0.copy() A A0.copy() A0.copy A0 copy for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
     i range(n - 1) range n - 1 n 1 for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
     j range(n - 1, i, -1) range n - 1 n 1 i -1 if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
     A[j].v < A[j - 1].v A[j].v [j] A j v A[j - 1].v [j - 1] A j - 1 j 1 v 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, True return A, True A True A0 A0 def selection_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
    return A selection_sort A = A0.copy() A A0.copy() A0.copy A0 copy for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
     i range(n - 1) range n - 1 n 1 k = i k i for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
         j range(i + 1, n) range i + 1 i 1 n if A[k].v > A[j].v:
                k = j
         A[k].v > A[j].v A[k].v [k] A k v A[j].v [j] A j v k = j k j if k != i:
            A[k], A[i] = A[i], A[k]
     k != i k i A[k], A[i] = A[i], A[k] A[k] A k A[i] A i A[i] A i A[k] A k return A A A0 A0 lst = ["Not stable", "Stable"] lst ["Not stable", "Stable"] "Not stable" "Stable" n = int(input()) n int(input()) int input() input C(x[0], x[1]) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() C(x[0], x[1]) C x[0] x 0 x[1] x 1 A = [C(x[0], x[1]) for x in input().split()] A [C(x[0], x[1]) for x in input().split()] B, j = bubble_sort(A) B bubble_sort(A) bubble_sort A j (B[i].m + B[i].v) for i in range(n) for i in range(n) i range(n) range n for i in range(n) B[i].m + B[i].v B[i].m [i] B i m B[i].v [i] B i v print(*[(B[i].m + B[i].v) for i in range(n)]) print *[(B[i].m + B[i].v) for i in range(n)] [(B[i].m + B[i].v) for i in range(n)] print(lst[j]) print lst[j] lst j S = selection_sort(A) S selection_sort(A) selection_sort A (S[i].m + S[i].v) for i in range(n) for i in range(n) i range(n) range n for i in range(n) S[i].m + S[i].v S[i].m [i] S i m S[i].v [i] S i v print(*[(S[i].m + S[i].v) for i in range(n)]) print *[(S[i].m + S[i].v) for i in range(n)] [(S[i].m + S[i].v) for i in range(n)] print(lst[B == S]) print lst[B == S] lst B == S B S A = [C(x[0], x[1]) for x in input().split()] [C(x[0], x[1]) for x in input().split()] A B, j = bubble_sort(A) bubble_sort(A) B def selection_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
    return A def selection_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
    return A selection_sort lst = ["Not stable", "Stable"] ["Not stable", "Stable"] lst S = selection_sort(A) selection_sort(A) S C = namedtuple("C", "m v") namedtuple("C", "m v") C def bubble_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
    return A, True def bubble_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
    return A, True bubble_sort n = int(input()) int(input()) n j = bubble_sort(A) bubble_sort(A) j
from collections import namedtuple
C = namedtuple("C", "m v")


def bubble_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        for j in range(n - 1, i, -1):
            if A[j].v < A[j - 1].v:
                A[j], A[j - 1] = A[j - 1], A[j]
    return A, True


def selection_sort(A0):
    A = A0.copy()
    for i in range(n - 1):
        k = i
        for j in range(i + 1, n):
            if A[k].v > A[j].v:
                k = j
        if k != i:
            A[k], A[i] = A[i], A[k]
    return A


lst = ["Not stable", "Stable"]
n = int(input())
A = [C(x[0], x[1]) for x in input().split()]

B, j = bubble_sort(A)
print(*[(B[i].m + B[i].v) for i in range(n)])
print(lst[j])

S = selection_sort(A)
print(*[(S[i].m + S[i].v) for i in range(n)])
print(lst[B == S])

