#! python3
# stable_sort.py

import copy

def num_from_card(card):
    return int(card[1:])

def bubble_sort(A, N):
    A_ = copy.copy(A)
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
    return A_

def selection_sort(A, N):
    A_ = copy.copy(A)
    for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
    return A_

def is_stable(A, sorted_A, N):
    A_nums = [int(x[1:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
    return flag

N = int(input())
cards = [x for x in input().split(' ')]

bubble_sorted = bubble_sort(cards, N)
selection_sorted = selection_sort(cards, N)

print(' '.join(bubble_sorted))
print(is_stable(cards, bubble_sorted, N))
print(' '.join(selection_sorted))
print(is_stable(cards, selection_sorted, N)) import copy copy def num_from_card(card):
    return int(card[1:]) num_from_card return int(card[1:]) int(card[1:]) int card[1:] card 1: 1 card card def bubble_sort(A, N):
    A_ = copy.copy(A)
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
    return A_ bubble_sort A_ = copy.copy(A) A_ copy.copy(A) copy.copy copy copy A flag = True flag True while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
     flag flag = False flag False for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
     i range(N-1, 0, -1) range N-1 N 1 0 -1 if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
     num_from_card(A_[i]) < num_from_card(A_[i-1]) num_from_card(A_[i]) num_from_card A_[i] A_ i num_from_card(A_[i-1]) num_from_card A_[i-1] A_ i-1 i 1 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 flag = True flag True return A_ A_ A A N N def selection_sort(A, N):
    A_ = copy.copy(A)
    for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
    return A_ selection_sort A_ = copy.copy(A) A_ copy.copy(A) copy.copy copy copy A for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
     i range(N-1) range N-1 N 1 minj = i minj i for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
         j range(i, N) range i N if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
         num_from_card(A_[j]) < num_from_card(A_[minj]) num_from_card(A_[j]) num_from_card A_[j] A_ j num_from_card(A_[minj]) num_from_card A_[minj] A_ minj minj = j minj j if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
     i != minj i minj A_[i], A_[minj] = A_[minj], A_[i] A_[i] A_ i A_[minj] A_ minj A_[minj] A_ minj A_[i] A_ i return A_ A_ A A N N def is_stable(A, sorted_A, N):
    A_nums = [int(x[1:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
    return flag is_stable int(x[1:]) for x in A for x in A x A for x in A int(x[1:]) int x[1:] x 1: 1 A_nums = [int(x[1:]) for x in A] A_nums [int(x[1:]) for x in A] x for x in set(A_nums) if A_nums.count(x) > 1 for x in set(A_nums) if A_nums.count(x) > 1 x set(A_nums) set A_nums A_nums.count(x) > 1 A_nums.count(x) A_nums.count A_nums count x 1 if A_nums.count(x) > 1 x same_num = [x for x in set(A_nums) if A_nums.count(x) > 1] same_num [x for x in set(A_nums) if A_nums.count(x) > 1] A_symbols, sorted_A_symbols = [], [] A_symbols [] sorted_A_symbols [] flag = 'Stable' flag 'Stable' for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
     num same_num if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
     [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num] [card[0] for card in A if int(card[1:]) == num] [card[0] for card in sorted_A if int(card[1:]) == num] flag = 'Not stable' flag 'Not stable' break return flag flag A A sorted_A sorted_A N N N = int(input()) N int(input()) int input() input x for x in input().split(' ') for x in input().split(' ') x input().split(' ') ().split () input split ' ' for x in input().split(' ') x cards = [x for x in input().split(' ')] cards [x for x in input().split(' ')] bubble_sorted = bubble_sort(cards, N) bubble_sorted bubble_sort(cards, N) bubble_sort cards N selection_sorted = selection_sort(cards, N) selection_sorted selection_sort(cards, N) selection_sort cards N print(' '.join(bubble_sorted)) print ' '.join(bubble_sorted) ' '.join ' ' join bubble_sorted print(is_stable(cards, bubble_sorted, N)) print is_stable(cards, bubble_sorted, N) is_stable cards bubble_sorted N print(' '.join(selection_sorted)) print ' '.join(selection_sorted) ' '.join ' ' join selection_sorted print(is_stable(cards, selection_sorted, N)) print is_stable(cards, selection_sorted, N) is_stable cards selection_sorted N def is_stable(A, sorted_A, N):
    A_nums = [int(x[1:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
    return flag def is_stable(A, sorted_A, N):
    A_nums = [int(x[1:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
    return flag is_stable def selection_sort(A, N):
    A_ = copy.copy(A)
    for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
    return A_ def selection_sort(A, N):
    A_ = copy.copy(A)
    for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
    return A_ selection_sort cards = [x for x in input().split(' ')] [x for x in input().split(' ')] cards bubble_sorted = bubble_sort(cards, N) bubble_sort(cards, N) bubble_sorted def bubble_sort(A, N):
    A_ = copy.copy(A)
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
    return A_ def bubble_sort(A, N):
    A_ = copy.copy(A)
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
    return A_ bubble_sort N = int(input()) int(input()) N selection_sorted = selection_sort(cards, N) selection_sort(cards, N) selection_sorted def num_from_card(card):
    return int(card[1:]) def num_from_card(card):
    return int(card[1:]) num_from_card
#! python3
# stable_sort.py

import copy

def num_from_card(card):
    return int(card[1:])

def bubble_sort(A, N):
    A_ = copy.copy(A)
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if num_from_card(A_[i]) < num_from_card(A_[i-1]):
                A_[i-1], A_[i] = A_[i], A_[i-1]
                flag = True
    return A_

def selection_sort(A, N):
    A_ = copy.copy(A)
    for i in range(N-1):
        minj = i
        for j in range(i, N):
            if num_from_card(A_[j]) < num_from_card(A_[minj]):
                minj = j
        if i != minj:
            A_[i], A_[minj] = A_[minj], A_[i]
    return A_

def is_stable(A, sorted_A, N):
    A_nums = [int(x[1:]) for x in A]
    same_num = [x for x in set(A_nums) if A_nums.count(x) > 1]
    A_symbols, sorted_A_symbols = [], []
    flag = 'Stable'
    for num in same_num:
        if [card[0] for card in A if int(card[1:]) == num] != [card[0] for card in sorted_A if int(card[1:]) == num]:
            flag = 'Not stable'
            break
    return flag

N = int(input())
cards = [x for x in input().split(' ')]

bubble_sorted = bubble_sort(cards, N)
selection_sorted = selection_sort(cards, N)

print(' '.join(bubble_sorted))
print(is_stable(cards, bubble_sorted, N))
print(' '.join(selection_sorted))
print(is_stable(cards, selection_sorted, N))
