def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c

def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	return c

n = int(input())
card = list(map(str,input().split()))
card1 = card[:]
bubble(card)
selection(card1)
print(" ".join(map(str,card)))
print("Stable")
print(" ".join(map(str,card1)))
print("Stable") if card==card1 else print("Not stable") def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c bubble for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	 i range(len(c)) range len(c) len c for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	 j range(len(c)-1,i,-1) range len(c)-1 len(c) len c 1 i -1 if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	 int(c[j][-1]) < int(c[j-1][-1]) int(c[j][-1]) int c[j][-1] [j] c j -1 int(c[j-1][-1]) int c[j-1][-1] [j-1] c j-1 j 1 -1 c[j],c[j-1] = c[j-1],c[j] c[j] c j c[j-1] c j-1 j 1 c[j-1] c j-1 j 1 c[j] c j return c c c c def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	return c selection for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	 i range(len(c)) range len(c) len c mini = i mini i for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		 j range(i,len(c)) range i len(c) len c if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		 int(c[j][-1])<int(c[mini][-1]) int(c[j][-1]) int c[j][-1] [j] c j -1 int(c[mini][-1]) int c[mini][-1] [mini] c mini -1 mini = j mini j c[i],c[mini]=c[mini],c[i] c[i] c i c[mini] c mini c[mini] c mini c[i] c i return c c c c n = int(input()) n int(input()) int input() input card = list(map(str,input().split())) card list(map(str,input().split())) list map(str,input().split()) map str input().split() ().split () input split card1 = card[:] card1 card[:] card : bubble(card) bubble card selection(card1) selection card1 print(" ".join(map(str,card))) print " ".join(map(str,card)) " ".join " " join map(str,card) map str card print("Stable") print "Stable" print(" ".join(map(str,card1))) print " ".join(map(str,card1)) " ".join " " join map(str,card1) map str card1 print("Stable") if card==card1 else print("Not stable") card==card1 card card1 print("Stable") print "Stable" print("Not stable") print "Not stable" def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c bubble def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	return c def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	return c selection card1 = card[:] card[:] card1 card = list(map(str,input().split())) list(map(str,input().split())) card n = int(input()) int(input()) n
def bubble(c):
	for i in range(len(c)):
		for j in range(len(c)-1,i,-1):
			if int(c[j][-1]) < int(c[j-1][-1]):
				c[j],c[j-1] = c[j-1],c[j]
	return c

def selection(c):
	for i in range(len(c)):
		mini = i
		for j in range(i,len(c)):
			if int(c[j][-1])<int(c[mini][-1]):
				mini = j
		c[i],c[mini]=c[mini],c[i]
	return c

n = int(input())
card = list(map(str,input().split()))
card1 = card[:]
bubble(card)
selection(card1)
print(" ".join(map(str,card)))
print("Stable")
print(" ".join(map(str,card1)))
print("Stable") if card==card1 else print("Not stable")