def selection(l):
	lists = l[:]
	N = len(lists)

	for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	return lists


def bubble(l):
	lists = l[:]
	N = len(lists)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	return lists


def output(l):
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)




if __name__ == '__main__':
	n = int(input())
	l = input().split()
	l_1 = bubble(l)
	l_2 = selection(l)

	output(l_1)
	print("Stable")
	output(l_2)
	if l_1 == l_2:
		print("Stable")
	else:
		print("Not stable")
 def selection(l):
	lists = l[:]
	N = len(lists)

	for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	return lists selection lists = l[:] lists l[:] l : N = len(lists) N len(lists) len lists for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	 i range(N) range N minj = i minj i for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		 j range(i, N) range i N if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		 int(lists[j][1]) < int(lists[minj][1]) int(lists[j][1]) int lists[j][1] [j] lists j 1 int(lists[minj][1]) int lists[minj][1] [minj] lists minj 1 minj = j minj j if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	 i == minj i minj continue lists[i], lists[minj] = lists[minj], lists[i] lists[i] lists i lists[minj] lists minj lists[minj] lists minj lists[i] lists i return lists lists l l def bubble(l):
	lists = l[:]
	N = len(lists)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	return lists bubble lists = l[:] lists l[:] l : N = len(lists) N len(lists) len lists flag = 1 flag 1 while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	 flag flag = 0 flag 0 for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	 j range(N - 1, 1 - 1, -1) range N - 1 N 1 1 - 1 1 1 -1 if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	 int(lists[j][1]) < int(lists[j - 1][1]) int(lists[j][1]) int lists[j][1] [j] lists j 1 int(lists[j - 1][1]) int lists[j - 1][1] [j - 1] lists j - 1 j 1 1 lists[j], lists[j - 1] = lists[j - 1], lists[j] lists[j] lists j lists[j - 1] lists j - 1 j 1 lists[j - 1] lists j - 1 j 1 lists[j] lists j flag = 1 flag 1 return lists lists l l def output(l):
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i) output count = 0 count 0 leng = len(l) leng len(l) len l for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i) i l count += 1 count 1 if count < leng:
			print(i, end =' ')
		else:
			print(i) count < leng count leng print(i, end =' ') print i end =' ' end ' ' print(i) print i l l if __name__ == '__main__':
	n = int(input())
	l = input().split()
	l_1 = bubble(l)
	l_2 = selection(l)

	output(l_1)
	print("Stable")
	output(l_2)
	if l_1 == l_2:
		print("Stable")
	else:
		print("Not stable") __name__ == '__main__' __name__ '__main__' n = int(input()) n int(input()) int input() input l = input().split() l input().split() ().split () input split l_1 = bubble(l) l_1 bubble(l) bubble l l_2 = selection(l) l_2 selection(l) selection l output(l_1) output l_1 print("Stable") print "Stable" output(l_2) output l_2 if l_1 == l_2:
		print("Stable")
	else:
		print("Not stable") l_1 == l_2 l_1 l_2 print("Stable") print "Stable" print("Not stable") print "Not stable" def output(l):
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i) def output(l):
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i) output def bubble(l):
	lists = l[:]
	N = len(lists)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	return lists def bubble(l):
	lists = l[:]
	N = len(lists)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	return lists bubble l = input().split() input().split() l l_2 = selection(l) selection(l) l_2 n = int(input()) int(input()) n def selection(l):
	lists = l[:]
	N = len(lists)

	for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	return lists def selection(l):
	lists = l[:]
	N = len(lists)

	for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	return lists selection l_1 = bubble(l) bubble(l) l_1
def selection(l):
	lists = l[:]
	N = len(lists)

	for i in range(N):
		minj = i

		for j in range(i, N):
			if int(lists[j][1]) < int(lists[minj][1]):
				minj = j
		if i == minj:
			continue
		else:
			lists[i], lists[minj] = lists[minj], lists[i]

	return lists


def bubble(l):
	lists = l[:]
	N = len(lists)
	flag = 1

	while flag:
		flag = 0
		for j in range(N - 1, 1 - 1, -1):
			if int(lists[j][1]) < int(lists[j - 1][1]):
				lists[j], lists[j - 1] = lists[j - 1], lists[j]
				flag = 1

	return lists


def output(l):
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)




if __name__ == '__main__':
	n = int(input())
	l = input().split()
	l_1 = bubble(l)
	l_2 = selection(l)

	output(l_1)
	print("Stable")
	output(l_2)
	if l_1 == l_2:
		print("Stable")
	else:
		print("Not stable")

