def bubble_sort(c, n):
    c = c.copy()
    for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return c


def selection_sort(a, n):
    a = a.copy()
    for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
    return a


def print_list(a):
    print(" ".join([s + v for s, v in a]))


def is_stable(l1, l2):
    def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

    before = l2[0]
    for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

    return True


n = int(input())
tmp = input().split()
a = []
for v in tmp:
    a.append((v[0], v[1]))

bs_result = bubble_sort(a, n)
ss_result = selection_sort(a, n)

print_list(bs_result)
if is_stable(a, bs_result):
    print("Stable")
else:
    print("Not stable")

print_list(ss_result)
if is_stable(a, ss_result):
    print("Stable")
else:
    print("Not stable")
 def bubble_sort(c, n):
    c = c.copy()
    for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return c bubble_sort c = c.copy() c c.copy() c.copy c copy for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
     i range(n) range n for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
     j range(0, n - 1) range 0 n - 1 n 1 if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
     c[j][1] > c[j + 1][1] c[j][1] [j] c j 1 c[j + 1][1] [j + 1] c j + 1 j 1 1 c[j], c[j + 1] = c[j + 1], c[j] c[j] c j c[j + 1] c j + 1 j 1 c[j + 1] c j + 1 j 1 c[j] c j return c c c c n n def selection_sort(a, n):
    a = a.copy()
    for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
    return a selection_sort a = a.copy() a a.copy() a.copy a copy for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
     i range(n) range n min_index = i min_index i for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
         j range(i, n) range i n if a[j][1] < a[min_index][1]:
                min_index = j
         a[j][1] < a[min_index][1] a[j][1] [j] a j 1 a[min_index][1] [min_index] a min_index 1 min_index = j min_index j a[i], a[min_index] = a[min_index], a[i] a[i] a i a[min_index] a min_index a[min_index] a min_index a[i] a i return a a a a n n def print_list(a):
    print(" ".join([s + v for s, v in a])) print_list s + v for s, v in a for s, v in a s v a for s, v in a s + v s v print(" ".join([s + v for s, v in a])) print " ".join([s + v for s, v in a]) " ".join " " join [s + v for s, v in a] a a def is_stable(l1, l2):
    def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

    before = l2[0]
    for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

    return True is_stable def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

     get_original_index for i, e in enumerate(l1):
            if v == e:
                return i
         i e enumerate(l1) enumerate l1 if v == e:
                return i
         v == e v e return i i return Exception("bug") Exception("bug") Exception "bug" v v before = l2[0] before l2[0] l2 0 for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

     e l2[1:] l2 1: 1 if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
         before[1] == e[1] before[1] before 1 e[1] e 1 bi = get_original_index(before) bi get_original_index(before) get_original_index before ei = get_original_index(e) ei get_original_index(e) get_original_index e if bi > ei:
                return False
         bi > ei bi ei return False False before = e before e return True True l1 l1 l2 l2 n = int(input()) n int(input()) int input() input tmp = input().split() tmp input().split() ().split () input split a = [] a [] for v in tmp:
    a.append((v[0], v[1])) v tmp a.append((v[0], v[1])) a.append a append (v[0], v[1]) v[0] v 0 v[1] v 1 bs_result = bubble_sort(a, n) bs_result bubble_sort(a, n) bubble_sort a n ss_result = selection_sort(a, n) ss_result selection_sort(a, n) selection_sort a n print_list(bs_result) print_list bs_result if is_stable(a, bs_result):
    print("Stable")
else:
    print("Not stable") is_stable(a, bs_result) is_stable a bs_result print("Stable") print "Stable" print("Not stable") print "Not stable" print_list(ss_result) print_list ss_result if is_stable(a, ss_result):
    print("Stable")
else:
    print("Not stable") is_stable(a, ss_result) is_stable a ss_result print("Stable") print "Stable" print("Not stable") print "Not stable" bs_result = bubble_sort(a, n) bubble_sort(a, n) bs_result def selection_sort(a, n):
    a = a.copy()
    for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
    return a def selection_sort(a, n):
    a = a.copy()
    for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
    return a selection_sort a = [] [] a ss_result = selection_sort(a, n) selection_sort(a, n) ss_result def bubble_sort(c, n):
    c = c.copy()
    for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return c def bubble_sort(c, n):
    c = c.copy()
    for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return c bubble_sort def print_list(a):
    print(" ".join([s + v for s, v in a])) def print_list(a):
    print(" ".join([s + v for s, v in a])) print_list def is_stable(l1, l2):
    def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

    before = l2[0]
    for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

    return True def is_stable(l1, l2):
    def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

    before = l2[0]
    for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

    return True is_stable n = int(input()) int(input()) n tmp = input().split() input().split() tmp
def bubble_sort(c, n):
    c = c.copy()
    for i in range(n):
        for j in range(0, n - 1):
            if c[j][1] > c[j + 1][1]:
                c[j], c[j + 1] = c[j + 1], c[j]
    return c


def selection_sort(a, n):
    a = a.copy()
    for i in range(n):
        min_index = i
        for j in range(i, n):
            if a[j][1] < a[min_index][1]:
                min_index = j
        a[i], a[min_index] = a[min_index], a[i]
    return a


def print_list(a):
    print(" ".join([s + v for s, v in a]))


def is_stable(l1, l2):
    def get_original_index(v):
        for i, e in enumerate(l1):
            if v == e:
                return i
        return Exception("bug")

    before = l2[0]
    for e in l2[1:]:
        if before[1] == e[1]:
            # print(before, e)
            bi = get_original_index(before)
            ei = get_original_index(e)
            # print(bi, ei)
            if bi > ei:
                return False
        before = e

    return True


n = int(input())
tmp = input().split()
a = []
for v in tmp:
    a.append((v[0], v[1]))

bs_result = bubble_sort(a, n)
ss_result = selection_sort(a, n)

print_list(bs_result)
if is_stable(a, bs_result):
    print("Stable")
else:
    print("Not stable")

print_list(ss_result)
if is_stable(a, ss_result):
    print("Stable")
else:
    print("Not stable")

