# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C&lang=jp
sample_input = list(range(3))
sample_input[0] = '''5
H4 C9 S4 D2 C3'''
sample_input[1] = '''2
S1 H1'''
sample_input[2] = '''5
H4 C9 S4 D2 C3'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
import copy

def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
    
class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number)

def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
            
def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
        
def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result

def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output = output.rstrip()
    print(output)

num_of_data = int(input())
data_list_str = input().split()
data_cards = [Card(str) for str in data_list_str]
data_cards_bubble = copy.copy(data_cards)
data_cards_selection = copy.copy(data_cards)

mark_order_info = []
for number in [n+1 for n in range(13)]:
    mark_order_info.append(get_mark_order_info(data_cards, number))
    
bubble_sort(data_cards_bubble)
selection_sort(data_cards_selection)

mark_order_info_bubble = []
for number in [n+1 for n in range(13)]:
    mark_order_info_bubble.append(get_mark_order_info(data_cards_bubble, number))

mark_order_info_selection = []
for number in [n+1 for n in range(13)]:
    mark_order_info_selection.append(get_mark_order_info(data_cards_selection, number))
    
print_card_list(data_cards_bubble)
if (mark_order_info == mark_order_info_bubble):
    print('Stable')
else:
    print('Not stable')
    
print_card_list(data_cards_selection)
if (mark_order_info == mark_order_info_selection):
    print('Stable')
else:
    print('Not stable') sample_input = list(range(3)) sample_input list(range(3)) list range(3) range 3 sample_input[0] = '''5
H4 C9 S4 D2 C3''' sample_input[0] sample_input 0 '''5
H4 C9 S4 D2 C3''' sample_input[1] = '''2
S1 H1''' sample_input[1] sample_input 1 '''2
S1 H1''' sample_input[2] = '''5
H4 C9 S4 D2 C3''' sample_input[2] sample_input 2 '''5
H4 C9 S4 D2 C3''' give_sample_input = None give_sample_input None if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main give_sample_input is not None give_sample_input None sample_input_list = sample_input[give_sample_input].split('\n') sample_input_list sample_input[give_sample_input].split('\n') [give_sample_input].split [give_sample_input] sample_input give_sample_input split '\n' def input():
        return sample_input_list.pop(0)
        
# main input return sample_input_list.pop(0) sample_input_list.pop(0) sample_input_list.pop sample_input_list pop 0 import copy copy def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
     swap_list_item tmp = lst[i] tmp lst[i] lst i lst[i] = lst[j] lst[i] lst i lst[j] lst j lst[j] = tmp lst[j] lst j tmp lst lst i i j j class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number) Card number = None number None mark = None mark None def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
     __init__ self.number = int(str_card[1]) self.number self number int(str_card[1]) int str_card[1] str_card 1 self.mark = str_card[0] self.mark self mark str_card[0] str_card 0 self self str_card str_card def __eq__(self, other):
        return self.number == other.number
     __eq__ return self.number == other.number self.number == other.number self.number self number other.number other number self self other other def __lt__(self, other):
        return self.number < other.number
     __lt__ return self.number < other.number self.number < other.number self.number self number other.number other number self self other other def to_str(self):
        return self.mark + str(self.number) to_str return self.mark + str(self.number) self.mark + str(self.number) self.mark self mark str(self.number) str self.number self number self self def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
             selection_sort num_of_data = len(list_of_data) num_of_data len(list_of_data) len list_of_data for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
             i range(num_of_data) range num_of_data minj = i minj i for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
         j range(i, num_of_data) range i num_of_data if list_of_data[minj] > list_of_data[j]:
                minj = j
         list_of_data[minj] > list_of_data[j] list_of_data[minj] list_of_data minj list_of_data[j] list_of_data j minj = j minj j if not minj == i:
            swap_list_item(list_of_data, i, minj)
             not minj == i minj == i minj i swap_list_item(list_of_data, i, minj) swap_list_item list_of_data i minj list_of_data list_of_data def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
         bubble_sort flag = True flag True while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
         flag flag = False flag False n = num_of_data - 1 n num_of_data - 1 num_of_data 1 while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
         n >= 1 n 1 if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
             list_of_data[n] < list_of_data[n-1] list_of_data[n] list_of_data n list_of_data[n-1] list_of_data n-1 n 1 swap_list_item(list_of_data, n, n-1) swap_list_item list_of_data n n-1 n 1 flag = True flag True n -= 1 n 1 list_of_data list_of_data def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result get_mark_order_info result = '' result '' for card in card_list:
        if card.number == number:
            result += card.mark
     card card_list if card.number == number:
            result += card.mark
     card.number == number card.number card number number result += card.mark result card.mark card mark return result result card_list card_list number number def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output = output.rstrip()
    print(output) print_card_list output = '' output '' for card in card_list:
        output += card.to_str() + ' '
     card card_list output += card.to_str() + ' ' output card.to_str() + ' ' card.to_str() card.to_str card to_str ' ' output = output.rstrip() output output.rstrip() output.rstrip output rstrip print(output) print output card_list card_list num_of_data = int(input()) num_of_data int(input()) int input() input data_list_str = input().split() data_list_str input().split() ().split () input split Card(str) for str in data_list_str for str in data_list_str str data_list_str for str in data_list_str Card(str) Card str data_cards = [Card(str) for str in data_list_str] data_cards [Card(str) for str in data_list_str] data_cards_bubble = copy.copy(data_cards) data_cards_bubble copy.copy(data_cards) copy.copy copy copy data_cards data_cards_selection = copy.copy(data_cards) data_cards_selection copy.copy(data_cards) copy.copy copy copy data_cards mark_order_info = [] mark_order_info [] for number in [n+1 for n in range(13)]:
    mark_order_info.append(get_mark_order_info(data_cards, number))
     number [n+1 for n in range(13)] mark_order_info.append(get_mark_order_info(data_cards, number)) mark_order_info.append mark_order_info append get_mark_order_info(data_cards, number) get_mark_order_info data_cards number bubble_sort(data_cards_bubble) bubble_sort data_cards_bubble selection_sort(data_cards_selection) selection_sort data_cards_selection mark_order_info_bubble = [] mark_order_info_bubble [] for number in [n+1 for n in range(13)]:
    mark_order_info_bubble.append(get_mark_order_info(data_cards_bubble, number)) number [n+1 for n in range(13)] mark_order_info_bubble.append(get_mark_order_info(data_cards_bubble, number)) mark_order_info_bubble.append mark_order_info_bubble append get_mark_order_info(data_cards_bubble, number) get_mark_order_info data_cards_bubble number mark_order_info_selection = [] mark_order_info_selection [] for number in [n+1 for n in range(13)]:
    mark_order_info_selection.append(get_mark_order_info(data_cards_selection, number))
     number [n+1 for n in range(13)] mark_order_info_selection.append(get_mark_order_info(data_cards_selection, number)) mark_order_info_selection.append mark_order_info_selection append get_mark_order_info(data_cards_selection, number) get_mark_order_info data_cards_selection number print_card_list(data_cards_bubble) print_card_list data_cards_bubble if (mark_order_info == mark_order_info_bubble):
    print('Stable')
else:
    print('Not stable')
     mark_order_info == mark_order_info_bubble mark_order_info mark_order_info_bubble print('Stable') print 'Stable' print('Not stable') print 'Not stable' print_card_list(data_cards_selection) print_card_list data_cards_selection if (mark_order_info == mark_order_info_selection):
    print('Stable')
else:
    print('Not stable') mark_order_info == mark_order_info_selection mark_order_info mark_order_info_selection print('Stable') print 'Stable' print('Not stable') print 'Not stable' def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
     def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
     swap_list_item def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
         def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
         bubble_sort data_cards_selection = copy.copy(data_cards) copy.copy(data_cards) data_cards_selection def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result get_mark_order_info data_list_str = input().split() input().split() data_list_str give_sample_input = None None give_sample_input data_cards = [Card(str) for str in data_list_str] [Card(str) for str in data_list_str] data_cards mark_order_info_selection = [] [] mark_order_info_selection def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output = output.rstrip()
    print(output) def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output = output.rstrip()
    print(output) print_card_list def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
             def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
             selection_sort sample_input = list(range(3)) list(range(3)) sample_input class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number) class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number) Card mark_order_info = [] [] mark_order_info sample_input_list = sample_input[give_sample_input].split('\n') sample_input[give_sample_input].split('\n') sample_input_list data_cards_bubble = copy.copy(data_cards) copy.copy(data_cards) data_cards_bubble num_of_data = int(input()) int(input()) num_of_data mark_order_info_bubble = [] [] mark_order_info_bubble def input():
        return sample_input_list.pop(0)
        
# main def input():
        return sample_input_list.pop(0)
        
# main input
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_C&lang=jp
sample_input = list(range(3))
sample_input[0] = '''5
H4 C9 S4 D2 C3'''
sample_input[1] = '''2
S1 H1'''
sample_input[2] = '''5
H4 C9 S4 D2 C3'''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
import copy

def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp
    
class Card:
    number = None
    mark = None
    def __init__(self, str_card):
        self.number = int(str_card[1])
        self.mark = str_card[0]
        pass
    def __eq__(self, other):
        return self.number == other.number
    def __lt__(self, other):
        return self.number < other.number
    def to_str(self):
        return self.mark + str(self.number)

def selection_sort(list_of_data):
    num_of_data = len(list_of_data)
    for i in range(num_of_data):
        minj = i
        for j in range(i, num_of_data):
            if list_of_data[minj] > list_of_data[j]:
                minj = j
        if not minj == i:
            swap_list_item(list_of_data, i, minj)
            
def bubble_sort(list_of_data):
    flag = True
    while flag:
        flag = False
        n = num_of_data - 1
        while n >= 1:
            if list_of_data[n] < list_of_data[n-1]:
                swap_list_item(list_of_data, n, n-1)
                flag = True
            n -= 1
        
def get_mark_order_info(card_list, number):
    result = ''
    for card in card_list:
        if card.number == number:
            result += card.mark
    return result

def print_card_list(card_list):
    output = ''
    for card in card_list:
        output += card.to_str() + ' '
    output = output.rstrip()
    print(output)

num_of_data = int(input())
data_list_str = input().split()
data_cards = [Card(str) for str in data_list_str]
data_cards_bubble = copy.copy(data_cards)
data_cards_selection = copy.copy(data_cards)

mark_order_info = []
for number in [n+1 for n in range(13)]:
    mark_order_info.append(get_mark_order_info(data_cards, number))
    
bubble_sort(data_cards_bubble)
selection_sort(data_cards_selection)

mark_order_info_bubble = []
for number in [n+1 for n in range(13)]:
    mark_order_info_bubble.append(get_mark_order_info(data_cards_bubble, number))

mark_order_info_selection = []
for number in [n+1 for n in range(13)]:
    mark_order_info_selection.append(get_mark_order_info(data_cards_selection, number))
    
print_card_list(data_cards_bubble)
if (mark_order_info == mark_order_info_bubble):
    print('Stable')
else:
    print('Not stable')
    
print_card_list(data_cards_selection)
if (mark_order_info == mark_order_info_selection):
    print('Stable')
else:
    print('Not stable')