N = input()
n = list(raw_input().split())
b = list(n)
for i in range(N-1):
	for j in range(N-i-1):
		if b[j][1] > b[j+1][1]:
			w = b[j]
			b[j] = b[j+1]
			b[j+1] = w
print " ".join(b[i] for i in range(N))
print "Stable"
for i in range(N-1):
	m = i
	for j in range(i+1,N):
		if n[m][1] > n[j][1]:
			m = j
	if m != i:
		w = n[i]
		n[i] = n[m]
		n[m] = w
print " ".join(n[i] for i in range(N))
if b == n:
	print "Stable"
else:
	print "Not stable" N = input() N input() input n = list(raw_input().split()) n list(raw_input().split()) list raw_input().split() ().split () raw_input split b = list(n) b list(n) list n for i in range(N-1):
	for j in range(N-i-1):
		if b[j][1] > b[j+1][1]:
			w = b[j]
			b[j] = b[j+1]
			b[j+1] = w i range(N-1) range N-1 N 1 for j in range(N-i-1):
		if b[j][1] > b[j+1][1]:
			w = b[j]
			b[j] = b[j+1]
			b[j+1] = w j range(N-i-1) range N-i-1 N-i N i 1 if b[j][1] > b[j+1][1]:
			w = b[j]
			b[j] = b[j+1]
			b[j+1] = w b[j][1] > b[j+1][1] b[j][1] [j] b j 1 b[j+1][1] [j+1] b j+1 j 1 1 w = b[j] w b[j] b j b[j] = b[j+1] b[j] b j b[j+1] b j+1 j 1 b[j+1] = w b[j+1] b j+1 j 1 w print " ".join(b[i] for i in range(N)) " ".join " " join b[i] b i print for i in range(N-1):
	m = i
	for j in range(i+1,N):
		if n[m][1] > n[j][1]:
			m = j
	if m != i:
		w = n[i]
		n[i] = n[m]
		n[m] = w i range(N-1) range N-1 N 1 m = i m i for j in range(i+1,N):
		if n[m][1] > n[j][1]:
			m = j
	 j range(i+1,N) range i+1 i 1 N if n[m][1] > n[j][1]:
			m = j
	 n[m][1] > n[j][1] n[m][1] [m] n m 1 n[j][1] [j] n j 1 m = j m j if m != i:
		w = n[i]
		n[i] = n[m]
		n[m] = w m != i m i w = n[i] w n[i] n i n[i] = n[m] n[i] n i n[m] n m n[m] = w n[m] n m w print " ".join(n[i] for i in range(N)) " ".join " " join n[i] n i if b == n:
	print "Stable"
else:
	print "Not stable" b == n b n print print n = list(raw_input().split()) list(raw_input().split()) n b = list(n) list(n) b w = b[j] b[j] w m = j j m m = i i m N = input() input() N w = n[i] n[i] w
N = input()
n = list(raw_input().split())
b = list(n)
for i in range(N-1):
	for j in range(N-i-1):
		if b[j][1] > b[j+1][1]:
			w = b[j]
			b[j] = b[j+1]
			b[j+1] = w
print " ".join(b[i] for i in range(N))
print "Stable"
for i in range(N-1):
	m = i
	for j in range(i+1,N):
		if n[m][1] > n[j][1]:
			m = j
	if m != i:
		w = n[i]
		n[i] = n[m]
		n[m] = w
print " ".join(n[i] for i in range(N))
if b == n:
	print "Stable"
else:
	print "Not stable"