def selection_sort(A):
    steps = 0
    N = len(A)
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
    return A


def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
    return A

d = dict()
N = int(input())
A = [(int(elem[1:]), elem[0]) for elem in input().split()]

for elem in A:
    if elem[0] not in d:
        d[elem[0]] = elem[1]
    else:
        d[elem[0]] += elem[1]

is_stable = True
A_bubble = bubbleSort(A[:])
st = A_bubble[0][1]
cur_val = A_bubble[1][0]
prev_val = A_bubble[0][0]
for e in A_bubble[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val
[print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') if idx != len(A_bubble) - 1 else print(A_bubble[idx][1] + str(A_bubble[idx][0])) for idx in range(len(A_bubble))]
print('Stable' if is_stable else 'Not stable')

is_stable = True
A_slt = selection_sort(A[:])
st = A_slt[0][1]
cur_val = A_slt[1][0]
prev_val = A_slt[0][0]
for e in A_slt[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val

[print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') if idx != len(A_slt) - 1 else print(A_slt[idx][1] + str(A_slt[idx][0])) for idx in range(len(A_slt))]
print('Stable' if is_stable else 'Not stable')
#
# 5
# H4 C9 S4 D2 C3 def selection_sort(A):
    steps = 0
    N = len(A)
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
    return A selection_sort steps = 0 steps 0 N = len(A) N len(A) len A for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
     i range(0, N) range 0 N minj = i minj i for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
         j range(i, N) range i N if A[minj][0] > A[j][0]:
                minj = j
         A[minj][0] > A[j][0] A[minj][0] [minj] A minj 0 A[j][0] [j] A j 0 minj = j minj j if i != minj:
            steps += 1
         i != minj i minj steps += 1 steps 1 A[minj], A[i] = A[i], A[minj] A[minj] A minj A[i] A i A[i] A i A[minj] A minj return A A A A def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
    return A bubbleSort steps = 0 steps 0 flag = 1 flag 1 completed_idx = 0 completed_idx 0 while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
     flag flag = 0 flag 0 for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
         i range(len(A) - 1, completed_idx, -1) range len(A) - 1 len(A) len A 1 completed_idx -1 if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
         A[i - 1][0] > A[i][0] A[i - 1][0] [i - 1] A i - 1 i 1 0 A[i][0] [i] A i 0 A[i - 1], A[i] = A[i], A[i - 1] A[i - 1] A i - 1 i 1 A[i] A i A[i] A i A[i - 1] A i - 1 i 1 steps += 1 steps 1 flag = 1 flag 1 completed_idx += 1 completed_idx 1 return A A A A d = dict() d dict() dict N = int(input()) N int(input()) int input() input (int(elem[1:]), elem[0]) for elem in input().split() for elem in input().split() elem input().split() ().split () input split for elem in input().split() (int(elem[1:]), elem[0]) int(elem[1:]) int elem[1:] elem 1: 1 elem[0] elem 0 A = [(int(elem[1:]), elem[0]) for elem in input().split()] A [(int(elem[1:]), elem[0]) for elem in input().split()] for elem in A:
    if elem[0] not in d:
        d[elem[0]] = elem[1]
    else:
        d[elem[0]] += elem[1] elem A if elem[0] not in d:
        d[elem[0]] = elem[1]
    else:
        d[elem[0]] += elem[1] elem[0] not in d elem[0] elem 0 d d[elem[0]] = elem[1] d[elem[0]] d elem[0] elem 0 elem[1] elem 1 d[elem[0]] += elem[1] d[elem[0]] d elem[0] elem 0 elem[1] elem 1 is_stable = True is_stable True A_bubble = bubbleSort(A[:]) A_bubble bubbleSort(A[:]) bubbleSort A[:] A : st = A_bubble[0][1] st A_bubble[0][1] [0] A_bubble 0 1 cur_val = A_bubble[1][0] cur_val A_bubble[1][0] [1] A_bubble 1 0 prev_val = A_bubble[0][0] prev_val A_bubble[0][0] [0] A_bubble 0 0 for e in A_bubble[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val e A_bubble[1:] A_bubble 1: 1 cur_val = e[0] cur_val e[0] e 0 if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
     cur_val != prev_val cur_val prev_val if st != d[prev_val]:
            is_stable = False
            break
         st != d[prev_val] st d[prev_val] d prev_val is_stable = False is_stable False break st = '' st '' st += e[1] st e[1] e 1 prev_val = cur_val prev_val cur_val print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') if idx != len(A_bubble) - 1 else print(A_bubble[idx][1] + str(A_bubble[idx][0])) for idx in range(len(A_bubble)) for idx in range(len(A_bubble)) idx range(len(A_bubble)) range len(A_bubble) len A_bubble for idx in range(len(A_bubble)) print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') if idx != len(A_bubble) - 1 else print(A_bubble[idx][1] + str(A_bubble[idx][0])) idx != len(A_bubble) - 1 idx len(A_bubble) - 1 len(A_bubble) len A_bubble 1 print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') print A_bubble[idx][1] + str(A_bubble[idx][0]) A_bubble[idx][1] [idx] A_bubble idx 1 str(A_bubble[idx][0]) str A_bubble[idx][0] [idx] A_bubble idx 0 end=' ' end ' ' print(A_bubble[idx][1] + str(A_bubble[idx][0])) print A_bubble[idx][1] + str(A_bubble[idx][0]) A_bubble[idx][1] [idx] A_bubble idx 1 str(A_bubble[idx][0]) str A_bubble[idx][0] [idx] A_bubble idx 0 [print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') if idx != len(A_bubble) - 1 else print(A_bubble[idx][1] + str(A_bubble[idx][0])) for idx in range(len(A_bubble))] print('Stable' if is_stable else 'Not stable') print 'Stable' if is_stable else 'Not stable' is_stable 'Stable' 'Not stable' is_stable = True is_stable True A_slt = selection_sort(A[:]) A_slt selection_sort(A[:]) selection_sort A[:] A : st = A_slt[0][1] st A_slt[0][1] [0] A_slt 0 1 cur_val = A_slt[1][0] cur_val A_slt[1][0] [1] A_slt 1 0 prev_val = A_slt[0][0] prev_val A_slt[0][0] [0] A_slt 0 0 for e in A_slt[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val e A_slt[1:] A_slt 1: 1 cur_val = e[0] cur_val e[0] e 0 if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
     cur_val != prev_val cur_val prev_val if st != d[prev_val]:
            is_stable = False
            break
         st != d[prev_val] st d[prev_val] d prev_val is_stable = False is_stable False break st = '' st '' st += e[1] st e[1] e 1 prev_val = cur_val prev_val cur_val print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') if idx != len(A_slt) - 1 else print(A_slt[idx][1] + str(A_slt[idx][0])) for idx in range(len(A_slt)) for idx in range(len(A_slt)) idx range(len(A_slt)) range len(A_slt) len A_slt for idx in range(len(A_slt)) print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') if idx != len(A_slt) - 1 else print(A_slt[idx][1] + str(A_slt[idx][0])) idx != len(A_slt) - 1 idx len(A_slt) - 1 len(A_slt) len A_slt 1 print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') print A_slt[idx][1] + str(A_slt[idx][0]) A_slt[idx][1] [idx] A_slt idx 1 str(A_slt[idx][0]) str A_slt[idx][0] [idx] A_slt idx 0 end=' ' end ' ' print(A_slt[idx][1] + str(A_slt[idx][0])) print A_slt[idx][1] + str(A_slt[idx][0]) A_slt[idx][1] [idx] A_slt idx 1 str(A_slt[idx][0]) str A_slt[idx][0] [idx] A_slt idx 0 [print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') if idx != len(A_slt) - 1 else print(A_slt[idx][1] + str(A_slt[idx][0])) for idx in range(len(A_slt))] print('Stable' if is_stable else 'Not stable') print 'Stable' if is_stable else 'Not stable' is_stable 'Stable' 'Not stable' prev_val = cur_val cur_val prev_val is_stable = True True is_stable cur_val = A_slt[1][0] A_slt[1][0] cur_val def selection_sort(A):
    steps = 0
    N = len(A)
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
    return A def selection_sort(A):
    steps = 0
    N = len(A)
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
    return A selection_sort is_stable = False False is_stable prev_val = A_bubble[0][0] A_bubble[0][0] prev_val A_slt = selection_sort(A[:]) selection_sort(A[:]) A_slt d = dict() dict() d cur_val = A_bubble[1][0] A_bubble[1][0] cur_val is_stable = True True is_stable cur_val = e[0] e[0] cur_val cur_val = e[0] e[0] cur_val st = '' '' st st = A_bubble[0][1] A_bubble[0][1] st def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
    return A def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
    return A bubbleSort st += e[1] e[1] st A_bubble = bubbleSort(A[:]) bubbleSort(A[:]) A_bubble st = A_slt[0][1] A_slt[0][1] st st += e[1] e[1] st is_stable = False False is_stable prev_val = cur_val cur_val prev_val prev_val = A_slt[0][0] A_slt[0][0] prev_val N = int(input()) int(input()) N A = [(int(elem[1:]), elem[0]) for elem in input().split()] [(int(elem[1:]), elem[0]) for elem in input().split()] A st = '' '' st
def selection_sort(A):
    steps = 0
    N = len(A)
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[minj][0] > A[j][0]:
                minj = j
        if i != minj:
            steps += 1
        A[minj], A[i] = A[i], A[minj]

    # [print(A[idx], end=' ') if idx != len(A) - 1 else print(A[idx]) for idx in range(len(A))]
    # print(steps)
    return A


def bubbleSort(A):
    steps = 0
    flag = 1
    completed_idx = 0
    while flag:
        flag = 0
        for i in range(len(A) - 1, completed_idx, -1):
            if A[i - 1][0] > A[i][0]:
                A[i - 1], A[i] = A[i], A[i - 1]
                steps += 1
                flag = 1
        completed_idx += 1

    # for e in A[:-1]:
    #     print(e, end=' ')
    # print(A[-1])
    # print(steps)
    return A

d = dict()
N = int(input())
A = [(int(elem[1:]), elem[0]) for elem in input().split()]

for elem in A:
    if elem[0] not in d:
        d[elem[0]] = elem[1]
    else:
        d[elem[0]] += elem[1]

is_stable = True
A_bubble = bubbleSort(A[:])
st = A_bubble[0][1]
cur_val = A_bubble[1][0]
prev_val = A_bubble[0][0]
for e in A_bubble[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val
[print(A_bubble[idx][1] + str(A_bubble[idx][0]), end=' ') if idx != len(A_bubble) - 1 else print(A_bubble[idx][1] + str(A_bubble[idx][0])) for idx in range(len(A_bubble))]
print('Stable' if is_stable else 'Not stable')

is_stable = True
A_slt = selection_sort(A[:])
st = A_slt[0][1]
cur_val = A_slt[1][0]
prev_val = A_slt[0][0]
for e in A_slt[1:]:
    cur_val = e[0]
    if cur_val != prev_val:
        if st != d[prev_val]:
            is_stable = False
            break
        st = ''
    st += e[1]
    prev_val = cur_val

[print(A_slt[idx][1] + str(A_slt[idx][0]), end=' ') if idx != len(A_slt) - 1 else print(A_slt[idx][1] + str(A_slt[idx][0])) for idx in range(len(A_slt))]
print('Stable' if is_stable else 'Not stable')
#
# 5
# H4 C9 S4 D2 C3
