
def bubblesort(c, n):
	for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j]

def selectionsort(c, n):
	for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i]

def output(c):
	for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	print()

def main():
	n = int(input())
	data = input().split()
	cards = []

	for c in data:
		cards.append((c[0],int(c[1])))
	cards1 = cards[:]
	cards2 = cards[:]

	bubblesort(cards1, n)
	output(cards1)
	print("Stable")

	selectionsort(cards2, n)
	output(cards2)
	if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable")

if __name__ == "__main__":
	main() def bubblesort(c, n):
	for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] bubblesort for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] i range(n) range n for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] j range(n-1, 0, -1) range n-1 n 1 0 -1 if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] c[j][1] < c[j-1][1] c[j][1] [j] c j 1 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 c c n n def selectionsort(c, n):
	for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i] selectionsort for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i] i range(n) range n minj = i minj i for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		 j range(i, n) range i n if (c[j][1] < c[minj][1]):
				minj = j
		 c[j][1] < c[minj][1] c[j][1] [j] c j 1 c[minj][1] [minj] c minj 1 minj = j minj j c[i], c[minj] = c[minj], c[i] c[i] c i c[minj] c minj c[minj] c minj c[i] c i c c n n def output(c):
	for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	print() output for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	 i range(len(c)) range len(c) len c if i != 0: print(" ", end="")
		 i != 0 i 0 print("{}{}".format(c[i][0], c[i][1]), end="") print "{}{}".format(c[i][0], c[i][1]) "{}{}".format "{}{}" format c[i][0] [i] c i 0 c[i][1] [i] c i 1 end="" end "" print() print c c def main():
	n = int(input())
	data = input().split()
	cards = []

	for c in data:
		cards.append((c[0],int(c[1])))
	cards1 = cards[:]
	cards2 = cards[:]

	bubblesort(cards1, n)
	output(cards1)
	print("Stable")

	selectionsort(cards2, n)
	output(cards2)
	if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable") main n = int(input()) n int(input()) int input() input data = input().split() data input().split() ().split () input split cards = [] cards [] for c in data:
		cards.append((c[0],int(c[1])))
	 c data cards.append((c[0],int(c[1]))) cards.append cards append (c[0],int(c[1])) c[0] c 0 int(c[1]) int c[1] c 1 cards1 = cards[:] cards1 cards[:] cards : cards2 = cards[:] cards2 cards[:] cards : bubblesort(cards1, n) bubblesort cards1 n output(cards1) output cards1 print("Stable") print "Stable" selectionsort(cards2, n) selectionsort cards2 n output(cards2) output cards2 if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable") cards1 == cards2 cards1 cards2 print("Stable") print "Stable" print("Not stable") print "Not stable" if __name__ == "__main__":
	main() __name__ == "__main__" __name__ "__main__" main() main def main():
	n = int(input())
	data = input().split()
	cards = []

	for c in data:
		cards.append((c[0],int(c[1])))
	cards1 = cards[:]
	cards2 = cards[:]

	bubblesort(cards1, n)
	output(cards1)
	print("Stable")

	selectionsort(cards2, n)
	output(cards2)
	if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable") def main():
	n = int(input())
	data = input().split()
	cards = []

	for c in data:
		cards.append((c[0],int(c[1])))
	cards1 = cards[:]
	cards2 = cards[:]

	bubblesort(cards1, n)
	output(cards1)
	print("Stable")

	selectionsort(cards2, n)
	output(cards2)
	if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable") main def selectionsort(c, n):
	for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i] def selectionsort(c, n):
	for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i] selectionsort def bubblesort(c, n):
	for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] def bubblesort(c, n):
	for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j] bubblesort def output(c):
	for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	print() def output(c):
	for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	print() output

def bubblesort(c, n):
	for i in range(n):
		for j in range(n-1, 0, -1):
			if (c[j][1] < c[j-1][1]):
				c[j], c[j-1] = c[j-1], c[j]

def selectionsort(c, n):
	for i in range(n):
		minj = i
		for j in range(i, n):
			if (c[j][1] < c[minj][1]):
				minj = j
		c[i], c[minj] = c[minj], c[i]

def output(c):
	for i in range(len(c)):
		if i != 0: print(" ", end="")
		print("{}{}".format(c[i][0], c[i][1]), end="")
	print()

def main():
	n = int(input())
	data = input().split()
	cards = []

	for c in data:
		cards.append((c[0],int(c[1])))
	cards1 = cards[:]
	cards2 = cards[:]

	bubblesort(cards1, n)
	output(cards1)
	print("Stable")

	selectionsort(cards2, n)
	output(cards2)
	if (cards1 == cards2):
		print("Stable")
	else:
		print("Not stable")

if __name__ == "__main__":
	main()