import sys

class Card:
	def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	def __str__(self):
		return self.card

def print_cards(cards, cards_):
	n = len(cards)
	same = True
	for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	print()
	return same	

def swap(cards, i, j):
	temp = cards[i]
	cards[i] = cards[j]
	cards[j] = temp

def bubble_sort(cards):
	n = len(cards)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1)

def selection_sort(cards):
	n = len(cards)
	for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		
n = int(input())
input_list = list(map(str, input().split()))
cards1 = [None] * n
cards2 = [None] * n
for i in range(n):
	cards1[i] = Card(input_list[i])
	cards2[i] = Card(input_list[i])
	
bubble_sort(cards1)
selection_sort(cards2)

print_cards(cards1, None)
print('Stable')

stable = print_cards(cards2, cards1)
if stable == True:
	print('Stable')
else:
	print('Not stable')
 import sys sys class Card:
	def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	def __str__(self):
		return self.card Card def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	 __init__ self.card = card self.card self card card self.mark = card[0] self.mark self mark card[0] card 0 self.value = card[1] self.value self value card[1] card 1 self self card card def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	 equals if self.mark != other.mark: return False
		 self.mark != other.mark self.mark self mark other.mark other mark if self.value != other.value: return False
		 self.value != other.value self.value self value other.value other value return True True self self other other def __str__(self):
		return self.card __str__ return self.card self.card self card self self def print_cards(cards, cards_):
	n = len(cards)
	same = True
	for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	print()
	return same	 print_cards n = len(cards) n len(cards) len cards same = True same True for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	 i range(n) range n if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		 cards_ != None and cards[i].equals(cards_[i]) == False cards_ != None cards_ None cards[i].equals(cards_[i]) == False cards[i].equals(cards_[i]) [i].equals [i] cards i equals cards_[i] cards_ i False same = False same False sys.stdout.write(str(cards[i])) sys.stdout.write sys.stdout sys stdout write str(cards[i]) str cards[i] cards i if i != n - 1:
			sys.stdout.write(' ')
	 i != n - 1 i n - 1 n 1 sys.stdout.write(' ') sys.stdout.write sys.stdout sys stdout write ' ' print() print return same same cards cards cards_ cards_ def swap(cards, i, j):
	temp = cards[i]
	cards[i] = cards[j]
	cards[j] = temp swap temp = cards[i] temp cards[i] cards i cards[i] = cards[j] cards[i] cards i cards[j] cards j cards[j] = temp cards[j] cards j temp cards cards i i j j def bubble_sort(cards):
	n = len(cards)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) bubble_sort n = len(cards) n len(cards) len cards for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) i range(n) range n for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) j range(n - 1, i, -1) range n - 1 n 1 i -1 if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) cards[j].value < cards[j - 1].value cards[j].value [j] cards j value cards[j - 1].value [j - 1] cards j - 1 j 1 value swap(cards, j, j - 1) swap cards j j - 1 j 1 cards cards def selection_sort(cards):
	n = len(cards)
	for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		 selection_sort n = len(cards) n len(cards) len cards for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		 i range(n) range n mini = i mini i for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		 j range(i, n) range i n if cards[j].value < cards[mini].value:
				mini = j
		 cards[j].value < cards[mini].value cards[j].value [j] cards j value cards[mini].value [mini] cards mini value mini = j mini j if mini != i:
			swap(cards, i, mini)
		 mini != i mini i swap(cards, i, mini) swap cards i mini cards cards n = int(input()) n int(input()) int input() input input_list = list(map(str, input().split())) input_list list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split cards1 = [None] * n cards1 [None] * n [None] None n cards2 = [None] * n cards2 [None] * n [None] None n for i in range(n):
	cards1[i] = Card(input_list[i])
	cards2[i] = Card(input_list[i])
	 i range(n) range n cards1[i] = Card(input_list[i]) cards1[i] cards1 i Card(input_list[i]) Card input_list[i] input_list i cards2[i] = Card(input_list[i]) cards2[i] cards2 i Card(input_list[i]) Card input_list[i] input_list i bubble_sort(cards1) bubble_sort cards1 selection_sort(cards2) selection_sort cards2 print_cards(cards1, None) print_cards cards1 None print('Stable') print 'Stable' stable = print_cards(cards2, cards1) stable print_cards(cards2, cards1) print_cards cards2 cards1 if stable == True:
	print('Stable')
else:
	print('Not stable') stable == True stable True print('Stable') print 'Stable' print('Not stable') print 'Not stable' def bubble_sort(cards):
	n = len(cards)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) def bubble_sort(cards):
	n = len(cards)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1) bubble_sort cards1 = [None] * n [None] * n cards1 stable = print_cards(cards2, cards1) print_cards(cards2, cards1) stable class Card:
	def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	def __str__(self):
		return self.card class Card:
	def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	def __str__(self):
		return self.card Card def print_cards(cards, cards_):
	n = len(cards)
	same = True
	for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	print()
	return same	 def print_cards(cards, cards_):
	n = len(cards)
	same = True
	for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	print()
	return same	 print_cards def swap(cards, i, j):
	temp = cards[i]
	cards[i] = cards[j]
	cards[j] = temp def swap(cards, i, j):
	temp = cards[i]
	cards[i] = cards[j]
	cards[j] = temp swap cards2 = [None] * n [None] * n cards2 def selection_sort(cards):
	n = len(cards)
	for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		 def selection_sort(cards):
	n = len(cards)
	for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		 selection_sort n = int(input()) int(input()) n input_list = list(map(str, input().split())) list(map(str, input().split())) input_list
import sys

class Card:
	def __init__(self, card):
		self.card = card
		self.mark = card[0]
		self.value = card[1]
	
	def equals(self, other):
		if self.mark != other.mark: return False
		if self.value != other.value: return False
		return True

	def __str__(self):
		return self.card

def print_cards(cards, cards_):
	n = len(cards)
	same = True
	for i in range(n):
		if cards_ != None and cards[i].equals(cards_[i]) == False:
			same = False
		sys.stdout.write(str(cards[i])) 
		if i != n - 1:
			sys.stdout.write(' ')
	print()
	return same	

def swap(cards, i, j):
	temp = cards[i]
	cards[i] = cards[j]
	cards[j] = temp

def bubble_sort(cards):
	n = len(cards)
	for i in range(n):
		for j in range(n - 1, i, -1):
			if cards[j].value < cards[j - 1].value:
				swap(cards, j, j - 1)

def selection_sort(cards):
	n = len(cards)
	for i in range(n):
		mini = i
		for j  in range(i, n):
			if cards[j].value < cards[mini].value:
				mini = j
		if mini != i:
			swap(cards, i, mini)
		
n = int(input())
input_list = list(map(str, input().split()))
cards1 = [None] * n
cards2 = [None] * n
for i in range(n):
	cards1[i] = Card(input_list[i])
	cards2[i] = Card(input_list[i])
	
bubble_sort(cards1)
selection_sort(cards2)

print_cards(cards1, None)
print('Stable')

stable = print_cards(cards2, cards1)
if stable == True:
	print('Stable')
else:
	print('Not stable')

