def bubble_sort(a, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp


def selection_sort(a, n):
    is_stable = True

    for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

    return is_stable

def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
    print(a[-1]['design'] + str(a[-1]['value']))


n = int(input())

a = input().split()
for i in range(n):
    s = a[i]
    a[i] = {'design': s[0], 'value': int(s[1])}
b = a.copy()
c = a.copy()

bubble_sort(a, n)
print_list_split_whitespace(a)

print("Stable")

is_stable = selection_sort(b, n)
print_list_split_whitespace(b)

if is_stable:
    print("Stable")
else:
    print("Not stable") def bubble_sort(a, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp bubble_sort for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp i range(n) range n for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp j range(n - 1, i, -1) range n - 1 n 1 i -1 if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp a[j]['value'] < a[j - 1]['value'] a[j]['value'] [j] a j 'value' a[j - 1]['value'] [j - 1] a j - 1 j 1 'value' tmp = a[j] tmp a[j] a j a[j] = a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 a[j - 1] = tmp a[j - 1] a j - 1 j 1 tmp a a n n def selection_sort(a, n):
    is_stable = True

    for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

    return is_stable selection_sort is_stable = True is_stable True for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

     i range(n) range n minj = i minj i samei = n samei n for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
         j range(i+1, n, 1) range i+1 i 1 n 1 if a[j]['value'] == a[i]['value']:
                samei = j
             a[j]['value'] == a[i]['value'] a[j]['value'] [j] a j 'value' a[i]['value'] [i] a i 'value' samei = j samei j if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
         a[j]['value'] < a[minj]['value'] a[j]['value'] [j] a j 'value' a[minj]['value'] [minj] a minj 'value' minj = j minj j if samei < minj:
                    is_stable = False
         samei < minj samei minj is_stable = False is_stable False tmp = a[i] tmp a[i] a i a[i] = a[minj] a[i] a i a[minj] a minj a[minj] = tmp a[minj] a minj tmp return is_stable is_stable a a n n def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
    print(a[-1]['design'] + str(a[-1]['value'])) print_list_split_whitespace for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
     x a[:-1] a :-1 -1 print(x['design'] + str(x['value']), end=' ') print x['design'] + str(x['value']) x['design'] x 'design' str(x['value']) str x['value'] x 'value' end=' ' end ' ' print(a[-1]['design'] + str(a[-1]['value'])) print a[-1]['design'] + str(a[-1]['value']) a[-1]['design'] [-1] a -1 'design' str(a[-1]['value']) str a[-1]['value'] [-1] a -1 'value' a a n = int(input()) n int(input()) int input() input a = input().split() a input().split() ().split () input split for i in range(n):
    s = a[i]
    a[i] = {'design': s[0], 'value': int(s[1])} i range(n) range n s = a[i] s a[i] a i a[i] = {'design': s[0], 'value': int(s[1])} a[i] a i {'design': s[0], 'value': int(s[1])} 'design' 'design' s[0] s 0 'value' 'value' int(s[1]) int s[1] s 1 b = a.copy() b a.copy() a.copy a copy c = a.copy() c a.copy() a.copy a copy bubble_sort(a, n) bubble_sort a n print_list_split_whitespace(a) print_list_split_whitespace a print("Stable") print "Stable" is_stable = selection_sort(b, n) is_stable selection_sort(b, n) selection_sort b n print_list_split_whitespace(b) print_list_split_whitespace b if is_stable:
    print("Stable")
else:
    print("Not stable") is_stable print("Stable") print "Stable" print("Not stable") print "Not stable" def selection_sort(a, n):
    is_stable = True

    for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

    return is_stable def selection_sort(a, n):
    is_stable = True

    for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

    return is_stable selection_sort a = input().split() input().split() a c = a.copy() a.copy() c b = a.copy() a.copy() b n = int(input()) int(input()) n def bubble_sort(a, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp def bubble_sort(a, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp bubble_sort def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
    print(a[-1]['design'] + str(a[-1]['value'])) def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
    print(a[-1]['design'] + str(a[-1]['value'])) print_list_split_whitespace s = a[i] a[i] s is_stable = selection_sort(b, n) selection_sort(b, n) is_stable
def bubble_sort(a, n):
    for i in range(n):
        for j in range(n - 1, i, -1):
            if a[j]['value'] < a[j - 1]['value']:
                tmp = a[j]
                a[j] = a[j - 1]
                a[j - 1] = tmp


def selection_sort(a, n):
    is_stable = True

    for i in range(n):
        minj = i
        samei = n
        for j in range(i+1, n, 1):
            if a[j]['value'] == a[i]['value']:
                samei = j
            if a[j]['value'] < a[minj]['value']:
                minj = j
                if samei < minj:
                    is_stable = False
        tmp = a[i]
        a[i] = a[minj]
        a[minj] = tmp

    return is_stable

def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x['design'] + str(x['value']), end=' ')
    print(a[-1]['design'] + str(a[-1]['value']))


n = int(input())

a = input().split()
for i in range(n):
    s = a[i]
    a[i] = {'design': s[0], 'value': int(s[1])}
b = a.copy()
c = a.copy()

bubble_sort(a, n)
print_list_split_whitespace(a)

print("Stable")

is_stable = selection_sort(b, n)
print_list_split_whitespace(b)

if is_stable:
    print("Stable")
else:
    print("Not stable")
