def bubble_sort(array):
    n = len(array)

    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
    return array

def selection_sort(array):
    n = len(array)

    for i in range(n):
        min_idx = i
        for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
    return array

def accum_hcsd_order(array):
    hcsd_order = {}
    for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
    return hcsd_order


def check_stable(array, hcsd_order):
    tgt_hcsd_order = accum_hcsd_order(array)

    for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
    return True


def main():
    n = int(input())
    array = [(x[0], int(x[1:])) for x in input().split()]
    str_stable_or_not = {True: "Stable", False: "Not stable"}

    hcsd_order = accum_hcsd_order(array)

    # bubble_sort
    array_b = array[:]
    array_b = bubble_sort(array_b)
    print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)))
    print(str_stable_or_not[check_stable(array_b, hcsd_order)])

    # selection_sort
    array_s = array[:]
    array_s = selection_sort(array_s)
    print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)))
    print(str_stable_or_not[check_stable(array_s, hcsd_order)])

    return

main() def bubble_sort(array):
    n = len(array)

    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
    return array bubble_sort n = len(array) n len(array) len array while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
     True flag = False flag False for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
         i range(1, n)[::-1] (1, n) range 1 n ::-1 -1 if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
         array[i-1][1] > array[i][1] array[i-1][1] [i-1] array i-1 i 1 1 array[i][1] [i] array i 1 tmp = array[i] tmp array[i] array i array[i] = array[i-1] array[i] array i array[i-1] array i-1 i 1 array[i-1] = tmp array[i-1] array i-1 i 1 tmp flag = True flag True if not flag:
            break
     not flag flag break return array array array array def selection_sort(array):
    n = len(array)

    for i in range(n):
        min_idx = i
        for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
    return array selection_sort n = len(array) n len(array) len array for i in range(n):
        min_idx = i
        for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
     i range(n) range n min_idx = i min_idx i for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
         j range(i, n) range i n if array[j][1] < array[min_idx][1]:
                min_idx = j
         array[j][1] < array[min_idx][1] array[j][1] [j] array j 1 array[min_idx][1] [min_idx] array min_idx 1 min_idx = j min_idx j if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
     min_idx != i min_idx i tmp = array[i] tmp array[i] array i array[i] = array[min_idx] array[i] array i array[min_idx] array min_idx array[min_idx] = tmp array[min_idx] array min_idx tmp return array array array array def accum_hcsd_order(array):
    hcsd_order = {}
    for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
    return hcsd_order accum_hcsd_order hcsd_order = {} hcsd_order {} for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
     item array if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
     item[1] in hcsd_order item[1] item 1 hcsd_order hcsd_order[item[1]].append(item[0]) [item[1]].append [item[1]] hcsd_order item[1] item 1 append item[0] item 0 hcsd_order[item[1]] = [item[0]] hcsd_order[item[1]] hcsd_order item[1] item 1 [item[0]] item[0] item 0 return hcsd_order hcsd_order array array def check_stable(array, hcsd_order):
    tgt_hcsd_order = accum_hcsd_order(array)

    for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
    return True check_stable tgt_hcsd_order = accum_hcsd_order(array) tgt_hcsd_order accum_hcsd_order(array) accum_hcsd_order array for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
     no hcsd hcsd_order.items() hcsd_order.items hcsd_order items if len(hcsd) == 1:
            continue
         len(hcsd) == 1 len(hcsd) len hcsd 1 continue tgt_hcsd = tgt_hcsd_order[no] tgt_hcsd tgt_hcsd_order[no] tgt_hcsd_order no for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
     src tgt zip(hcsd, tgt_hcsd) zip hcsd tgt_hcsd if src != tgt:
                return False
     src != tgt src tgt return False False return True True array array hcsd_order hcsd_order def main():
    n = int(input())
    array = [(x[0], int(x[1:])) for x in input().split()]
    str_stable_or_not = {True: "Stable", False: "Not stable"}

    hcsd_order = accum_hcsd_order(array)

    # bubble_sort
    array_b = array[:]
    array_b = bubble_sort(array_b)
    print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)))
    print(str_stable_or_not[check_stable(array_b, hcsd_order)])

    # selection_sort
    array_s = array[:]
    array_s = selection_sort(array_s)
    print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)))
    print(str_stable_or_not[check_stable(array_s, hcsd_order)])

    return main n = int(input()) n int(input()) int input() input (x[0], int(x[1:])) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() (x[0], int(x[1:])) x[0] x 0 int(x[1:]) int x[1:] x 1: 1 array = [(x[0], int(x[1:])) for x in input().split()] array [(x[0], int(x[1:])) for x in input().split()] str_stable_or_not = {True: "Stable", False: "Not stable"} str_stable_or_not {True: "Stable", False: "Not stable"} True True "Stable" False False "Not stable" hcsd_order = accum_hcsd_order(array) hcsd_order accum_hcsd_order(array) accum_hcsd_order array array_b = array[:] array_b array[:] array : array_b = bubble_sort(array_b) array_b bubble_sort(array_b) bubble_sort array_b print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b))) print " ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)) " ".join " " join map(lambda x: "{}{}".format(x[0],x[1]), array_b) map lambda x: "{}{}".format(x[0],x[1]) "{}{}".format(x[0],x[1]) "{}{}".format "{}{}" format x[0] x 0 x[1] x 1 x array_b print(str_stable_or_not[check_stable(array_b, hcsd_order)]) print str_stable_or_not[check_stable(array_b, hcsd_order)] str_stable_or_not check_stable(array_b, hcsd_order) check_stable array_b hcsd_order array_s = array[:] array_s array[:] array : array_s = selection_sort(array_s) array_s selection_sort(array_s) selection_sort array_s print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s))) print " ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)) " ".join " " join map(lambda x: "{}{}".format(x[0], x[1]), array_s) map lambda x: "{}{}".format(x[0], x[1]) "{}{}".format(x[0], x[1]) "{}{}".format "{}{}" format x[0] x 0 x[1] x 1 x array_s print(str_stable_or_not[check_stable(array_s, hcsd_order)]) print str_stable_or_not[check_stable(array_s, hcsd_order)] str_stable_or_not check_stable(array_s, hcsd_order) check_stable array_s hcsd_order return main() main def accum_hcsd_order(array):
    hcsd_order = {}
    for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
    return hcsd_order def accum_hcsd_order(array):
    hcsd_order = {}
    for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
    return hcsd_order accum_hcsd_order def selection_sort(array):
    n = len(array)

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

    for i in range(n):
        min_idx = i
        for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
    return array selection_sort def check_stable(array, hcsd_order):
    tgt_hcsd_order = accum_hcsd_order(array)

    for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
    return True def check_stable(array, hcsd_order):
    tgt_hcsd_order = accum_hcsd_order(array)

    for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
    return True check_stable def bubble_sort(array):
    n = len(array)

    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
    return array def bubble_sort(array):
    n = len(array)

    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
    return array bubble_sort def main():
    n = int(input())
    array = [(x[0], int(x[1:])) for x in input().split()]
    str_stable_or_not = {True: "Stable", False: "Not stable"}

    hcsd_order = accum_hcsd_order(array)

    # bubble_sort
    array_b = array[:]
    array_b = bubble_sort(array_b)
    print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)))
    print(str_stable_or_not[check_stable(array_b, hcsd_order)])

    # selection_sort
    array_s = array[:]
    array_s = selection_sort(array_s)
    print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)))
    print(str_stable_or_not[check_stable(array_s, hcsd_order)])

    return def main():
    n = int(input())
    array = [(x[0], int(x[1:])) for x in input().split()]
    str_stable_or_not = {True: "Stable", False: "Not stable"}

    hcsd_order = accum_hcsd_order(array)

    # bubble_sort
    array_b = array[:]
    array_b = bubble_sort(array_b)
    print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)))
    print(str_stable_or_not[check_stable(array_b, hcsd_order)])

    # selection_sort
    array_s = array[:]
    array_s = selection_sort(array_s)
    print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)))
    print(str_stable_or_not[check_stable(array_s, hcsd_order)])

    return main
def bubble_sort(array):
    n = len(array)

    while True:
        flag = False
        for i in range(1, n)[::-1]:
            if array[i-1][1] > array[i][1]:
                tmp = array[i]
                array[i] = array[i-1]
                array[i-1] = tmp
                flag = True
        if not flag:
            break
    return array

def selection_sort(array):
    n = len(array)

    for i in range(n):
        min_idx = i
        for j in range(i, n):
            if array[j][1] < array[min_idx][1]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
    return array

def accum_hcsd_order(array):
    hcsd_order = {}
    for item in array:
        if item[1] in hcsd_order:
            hcsd_order[item[1]].append(item[0])
        else:
            hcsd_order[item[1]] = [item[0]]
    return hcsd_order


def check_stable(array, hcsd_order):
    tgt_hcsd_order = accum_hcsd_order(array)

    for no, hcsd in hcsd_order.items():
        if len(hcsd) == 1:
            continue
        tgt_hcsd = tgt_hcsd_order[no]
        for src, tgt in zip(hcsd, tgt_hcsd):
            if src != tgt:
                return False
    return True


def main():
    n = int(input())
    array = [(x[0], int(x[1:])) for x in input().split()]
    str_stable_or_not = {True: "Stable", False: "Not stable"}

    hcsd_order = accum_hcsd_order(array)

    # bubble_sort
    array_b = array[:]
    array_b = bubble_sort(array_b)
    print(" ".join(map(lambda x: "{}{}".format(x[0],x[1]), array_b)))
    print(str_stable_or_not[check_stable(array_b, hcsd_order)])

    # selection_sort
    array_s = array[:]
    array_s = selection_sort(array_s)
    print(" ".join(map(lambda x: "{}{}".format(x[0], x[1]), array_s)))
    print(str_stable_or_not[check_stable(array_s, hcsd_order)])

    return

main()
