import sys

def bubble_sort(C, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	return C

def	selection_sort(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]
	
	return C

#fin = open("test.txt", "r")
fin = sys.stdin

N = int(fin.readline())
A = fin.readline().split()
B = A[:]

A = bubble_sort(A, N)
B = selection_sort(B, N)

print(" ".join(A))
print("Stable")

print(" ".join(B))
if A == B:
	print("Stable")
else:
	print("Not stable") import sys sys def bubble_sort(C, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	return C bubble_sort for i in range(N):
		for j in range(N - 1, i, -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, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	 j range(N - 1, i, -1) range N - 1 N 1 i -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 return C C C C N N def	selection_sort(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]
	
	return C

#fin = open("test.txt", "r") selection_sort 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 return C C C C N N fin = sys.stdin fin sys.stdin sys stdin N = int(fin.readline()) N int(fin.readline()) int fin.readline() fin.readline fin readline A = fin.readline().split() A fin.readline().split() fin.readline().split fin.readline() fin.readline fin readline split B = A[:] B A[:] A : A = bubble_sort(A, N) A bubble_sort(A, N) bubble_sort A N B = selection_sort(B, N) B selection_sort(B, N) selection_sort B N print(" ".join(A)) print " ".join(A) " ".join " " join A print("Stable") print "Stable" print(" ".join(B)) print " ".join(B) " ".join " " join B if A == B:
	print("Stable")
else:
	print("Not stable") A == B A B print("Stable") print "Stable" print("Not stable") print "Not stable" B = A[:] A[:] B def bubble_sort(C, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	return C def bubble_sort(C, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	return C bubble_sort N = int(fin.readline()) int(fin.readline()) N def	selection_sort(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]
	
	return C

#fin = open("test.txt", "r") def	selection_sort(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]
	
	return C

#fin = open("test.txt", "r") selection_sort A = fin.readline().split() fin.readline().split() A B = selection_sort(B, N) selection_sort(B, N) B fin = sys.stdin sys.stdin fin A = bubble_sort(A, N) bubble_sort(A, N) A
import sys

def bubble_sort(C, N):
	for i in range(N):
		for j in range(N - 1, i, -1):
			if C[j][1] < C[j - 1][1]:
				C[j], C[j - 1] = C[j - 1], C[j]
				
	return C

def	selection_sort(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]
	
	return C

#fin = open("test.txt", "r")
fin = sys.stdin

N = int(fin.readline())
A = fin.readline().split()
B = A[:]

A = bubble_sort(A, N)
B = selection_sort(B, N)

print(" ".join(A))
print("Stable")

print(" ".join(B))
if A == B:
	print("Stable")
else:
	print("Not stable")