# -*- coding: utf-8 -*-


def BubbleSort(C, N):
    flag = 1
    while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
    return C


def SelectionSort(C, N):
    flag = 0

    for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

    return C


def print_line(A):
    for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='')


def check_stable(Bubble_A, Select_A):
    for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable')


N = int(input())
A = list(input())

A = [A[i:i+2] for i in range(0, len(A), 3)]

Bubble_A = BubbleSort(A[:], N)
Select_A = SelectionSort(A[:], N)

print_line(Bubble_A)
print('Stable')
print_line(Select_A)
check_stable(Bubble_A, Select_A)
 def BubbleSort(C, N):
    flag = 1
    while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
    return C BubbleSort flag = 1 flag 1 while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
     flag == 1 flag 1 flag = 0 flag 0 for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
     i range(N - 1, 0, -1) range N - 1 N 1 0 -1 if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
     int(C[i][1]) < int(C[i - 1][1]) int(C[i][1]) int C[i][1] [i] C i 1 int(C[i - 1][1]) int C[i - 1][1] [i - 1] C i - 1 i 1 1 C[i], C[i - 1] = C[i - 1], C[i] C[i] C i C[i - 1] C i - 1 i 1 C[i - 1] C i - 1 i 1 C[i] C i flag = 1 flag 1 return C C C C N N def SelectionSort(C, N):
    flag = 0

    for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

    return C SelectionSort flag = 0 flag 0 for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

     i range(N) range N if N == 1:
            break

         N == 1 N 1 break minj = i minj i for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

         j range(i, N) range i N if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

         int(C[j][1]) < int(C[minj][1]) int(C[j][1]) int C[j][1] [j] C j 1 int(C[minj][1]) int C[minj][1] [minj] C minj 1 minj = j minj j flag = 1 flag 1 if flag == 1:
            C[i], C[minj] = C[minj], C[i]

         flag == 1 flag 1 C[i], C[minj] = C[minj], C[i] C[i] C i C[minj] C minj C[minj] C minj C[i] C i flag = 0 flag 0 return C C C C N N def print_line(A):
    for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='') print_line for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='') i range(len(A)) range len(A) len A if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='') i == len(A) - 1 i len(A) - 1 len(A) len A 1 print('{0}{1}'.format(A[i][0], A[i][1])) print '{0}{1}'.format(A[i][0], A[i][1]) '{0}{1}'.format '{0}{1}' format A[i][0] [i] A i 0 A[i][1] [i] A i 1 print('{0}{1} '.format(A[i][0], A[i][1]), end='') print '{0}{1} '.format(A[i][0], A[i][1]) '{0}{1} '.format '{0}{1} ' format A[i][0] [i] A i 0 A[i][1] [i] A i 1 end='' end '' A A def check_stable(Bubble_A, Select_A):
    for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable') check_stable for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable') i range(len(Select_A)) range len(Select_A) len Select_A if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable') Bubble_A[i][0] != Select_A[i][0] Bubble_A[i][0] [i] Bubble_A i 0 Select_A[i][0] [i] Select_A i 0 print('Not stable') print 'Not stable' break elif i == len(Select_A) - 1:
            print('Stable') i == len(Select_A) - 1 i len(Select_A) - 1 len(Select_A) len Select_A 1 print('Stable') print 'Stable' Bubble_A Bubble_A Select_A Select_A N = int(input()) N int(input()) int input() input A = list(input()) A list(input()) list input() input A[i:i+2] for i in range(0, len(A), 3) for i in range(0, len(A), 3) i range(0, len(A), 3) range 0 len(A) len A 3 for i in range(0, len(A), 3) A[i:i+2] A i:i+2 i i+2 i 2 A = [A[i:i+2] for i in range(0, len(A), 3)] A [A[i:i+2] for i in range(0, len(A), 3)] Bubble_A = BubbleSort(A[:], N) Bubble_A BubbleSort(A[:], N) BubbleSort A[:] A : N Select_A = SelectionSort(A[:], N) Select_A SelectionSort(A[:], N) SelectionSort A[:] A : N print_line(Bubble_A) print_line Bubble_A print('Stable') print 'Stable' print_line(Select_A) print_line Select_A check_stable(Bubble_A, Select_A) check_stable Bubble_A Select_A def print_line(A):
    for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='') def print_line(A):
    for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='') print_line def check_stable(Bubble_A, Select_A):
    for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable') def check_stable(Bubble_A, Select_A):
    for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable') check_stable A = [A[i:i+2] for i in range(0, len(A), 3)] [A[i:i+2] for i in range(0, len(A), 3)] A A = list(input()) list(input()) A def SelectionSort(C, N):
    flag = 0

    for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

    return C def SelectionSort(C, N):
    flag = 0

    for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

    return C SelectionSort N = int(input()) int(input()) N Bubble_A = BubbleSort(A[:], N) BubbleSort(A[:], N) Bubble_A Select_A = SelectionSort(A[:], N) SelectionSort(A[:], N) Select_A def BubbleSort(C, N):
    flag = 1
    while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
    return C def BubbleSort(C, N):
    flag = 1
    while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
    return C BubbleSort
# -*- coding: utf-8 -*-


def BubbleSort(C, N):
    flag = 1
    while flag == 1:
        flag = 0
        for i in range(N - 1, 0, -1):
            if int(C[i][1]) < int(C[i - 1][1]):
                C[i], C[i - 1] = C[i - 1], C[i]
                flag = 1
    return C


def SelectionSort(C, N):
    flag = 0

    for i in range(N):

        if N == 1:
            break

        minj = i
        for j in range(i, N):
            if int(C[j][1]) < int(C[minj][1]):
                minj = j
                flag = 1

        if flag == 1:
            C[i], C[minj] = C[minj], C[i]

        flag = 0

    return C


def print_line(A):
    for i in range(len(A)):
        if i == len(A) - 1:
            print('{0}{1}'.format(A[i][0], A[i][1]))
        else:
            print('{0}{1} '.format(A[i][0], A[i][1]), end='')


def check_stable(Bubble_A, Select_A):
    for i in range(len(Select_A)):
        if Bubble_A[i][0] != Select_A[i][0]:
            print('Not stable')
            break
        elif i == len(Select_A) - 1:
            print('Stable')


N = int(input())
A = list(input())

A = [A[i:i+2] for i in range(0, len(A), 3)]

Bubble_A = BubbleSort(A[:], N)
Select_A = SelectionSort(A[:], N)

print_line(Bubble_A)
print('Stable')
print_line(Select_A)
check_stable(Bubble_A, Select_A)

