def buble(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(raw_input())
card = map(str, raw_input().split())
card1 = card[:]
buble(card);selection(card1)
print " ".join(map(str, card))
print "Stable"
print " ".join(map(str, card1))
print "Stable" if card == card1 else "Not stable" def buble(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
	 buble 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(raw_input()) n int(raw_input()) int raw_input() raw_input card = map(str, raw_input().split()) card map(str, raw_input().split()) map str raw_input().split() ().split () raw_input split card1 = card[:] card1 card[:] card : buble(card) buble card selection(card1) selection card1 print " ".join(map(str, card)) " ".join " " join map(str, card) map str card print print " ".join(map(str, card1)) " ".join " " join map(str, card1) map str card1 print "Stable" if card == card1 else "Not stable" card == card1 card card1 "Stable" "Not stable" card = map(str, raw_input().split()) map(str, raw_input().split()) card def buble(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 buble(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
	 buble 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 n = int(raw_input()) int(raw_input()) n card1 = card[:] card[:] card1
def buble(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(raw_input())
card = map(str, raw_input().split())
card1 = card[:]
buble(card);selection(card1)
print " ".join(map(str, card))
print "Stable"
print " ".join(map(str, card1))
print "Stable" if card == card1 else "Not stable"