# coding: UTF-8

#共通関数と演算
def pprint(numlist):
  print(" ".join(map(str,numlist)))
def greater(s,t):
  if s[1] > t[1]:
    return True
  else:
    return False
def equal(s,t):
  if s[1] == t[1]:
    return True
  else:
    return False
def wasStable(Ans,Initial,N):
  I = Initial.copy()
  for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
  return True
  
#input
N = int(input())
A = input().split()
Initial = A.copy()
B = A.copy()

#バブルソート
count = 0
flag = True
while flag:
  flag = False
  j = N - 1
  while j >= 1:
    if greater(A[j-1],A[j]):
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1
pprint(A)
print("Stable")

#選択ソート
count = 0
for i in range(N):
  j=i
  minj=i
  while j<= N-1:
    if greater(B[minj],B[j]):
      minj = j
    j += 1
  if i != minj:
    v=B[i]
    B[i]=B[minj]
    B[minj]=v
    count += 1
    
print(" ".join(map(str,B)))
if wasStable(B,Initial,N):
  ans = "Stable"
else:
  ans = "Not stable"
print(ans)

    
 def pprint(numlist):
  print(" ".join(map(str,numlist))) pprint print(" ".join(map(str,numlist))) print " ".join(map(str,numlist)) " ".join " " join map(str,numlist) map str numlist numlist numlist def greater(s,t):
  if s[1] > t[1]:
    return True
  else:
    return False greater if s[1] > t[1]:
    return True
  else:
    return False s[1] > t[1] s[1] s 1 t[1] t 1 return True True return False False s s t t def equal(s,t):
  if s[1] == t[1]:
    return True
  else:
    return False equal if s[1] == t[1]:
    return True
  else:
    return False s[1] == t[1] s[1] s 1 t[1] t 1 return True True return False False s s t t def wasStable(Ans,Initial,N):
  I = Initial.copy()
  for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
  return True
  
#input wasStable I = Initial.copy() I Initial.copy() Initial.copy Initial copy for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
   i range(N-1) range N-1 N 1 if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
   equal(Ans[i],Ans[i+1]) equal Ans[i] Ans i Ans[i+1] Ans i+1 i 1 for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
   x I if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
   equal(Ans[i],x) equal Ans[i] Ans i x if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
   Ans[i][0] != x[0] Ans[i][0] [i] Ans i 0 x[0] x 0 return False False I.remove(x) I.remove I remove x break return True True Ans Ans Initial Initial N N N = int(input()) N int(input()) int input() input A = input().split() A input().split() ().split () input split Initial = A.copy() Initial A.copy() A.copy A copy B = A.copy() B A.copy() A.copy A copy count = 0 count 0 flag = True flag True while flag:
  flag = False
  j = N - 1
  while j >= 1:
    if greater(A[j-1],A[j]):
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1 flag flag = False flag False j = N - 1 j N - 1 N 1 while j >= 1:
    if greater(A[j-1],A[j]):
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1 j >= 1 j 1 if greater(A[j-1],A[j]):
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
     greater(A[j-1],A[j]) greater A[j-1] A j-1 j 1 A[j] A j v = A[j] v A[j] A j A[j] = A[j-1] A[j] A j A[j-1] A j-1 j 1 A[j-1] = v A[j-1] A j-1 j 1 v flag = True flag True count += 1 count 1 j -= 1 j 1 pprint(A) pprint A print("Stable") print "Stable" count = 0 count 0 for i in range(N):
  j=i
  minj=i
  while j<= N-1:
    if greater(B[minj],B[j]):
      minj = j
    j += 1
  if i != minj:
    v=B[i]
    B[i]=B[minj]
    B[minj]=v
    count += 1
     i range(N) range N j=i j i minj=i minj i while j<= N-1:
    if greater(B[minj],B[j]):
      minj = j
    j += 1
   j<= N-1 j N-1 N 1 if greater(B[minj],B[j]):
      minj = j
     greater(B[minj],B[j]) greater B[minj] B minj B[j] B j minj = j minj j j += 1 j 1 if i != minj:
    v=B[i]
    B[i]=B[minj]
    B[minj]=v
    count += 1
     i != minj i minj v=B[i] v B[i] B i B[i]=B[minj] B[i] B i B[minj] B minj B[minj]=v B[minj] B minj v count += 1 count 1 print(" ".join(map(str,B))) print " ".join(map(str,B)) " ".join " " join map(str,B) map str B if wasStable(B,Initial,N):
  ans = "Stable"
else:
  ans = "Not stable" wasStable(B,Initial,N) wasStable B Initial N ans = "Stable" ans "Stable" ans = "Not stable" ans "Not stable" print(ans) print ans flag = False False flag ans = "Not stable" "Not stable" ans j = N - 1 N - 1 j count += 1 1 count def wasStable(Ans,Initial,N):
  I = Initial.copy()
  for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
  return True
  
#input def wasStable(Ans,Initial,N):
  I = Initial.copy()
  for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
  return True
  
#input wasStable flag = True True flag def pprint(numlist):
  print(" ".join(map(str,numlist))) def pprint(numlist):
  print(" ".join(map(str,numlist))) pprint j += 1 1 j A = input().split() input().split() A N = int(input()) int(input()) N count = 0 0 count ans = "Stable" "Stable" ans def greater(s,t):
  if s[1] > t[1]:
    return True
  else:
    return False def greater(s,t):
  if s[1] > t[1]:
    return True
  else:
    return False greater flag = True True flag Initial = A.copy() A.copy() Initial minj=i i minj v=B[i] B[i] v v = A[j] A[j] v B = A.copy() A.copy() B j -= 1 1 j minj = j j minj def equal(s,t):
  if s[1] == t[1]:
    return True
  else:
    return False def equal(s,t):
  if s[1] == t[1]:
    return True
  else:
    return False equal j=i i j count = 0 0 count count += 1 1 count
# coding: UTF-8

#共通関数と演算
def pprint(numlist):
  print(" ".join(map(str,numlist)))
def greater(s,t):
  if s[1] > t[1]:
    return True
  else:
    return False
def equal(s,t):
  if s[1] == t[1]:
    return True
  else:
    return False
def wasStable(Ans,Initial,N):
  I = Initial.copy()
  for i in range(N-1):
    if equal(Ans[i],Ans[i+1]):
      for x in I:
        if equal(Ans[i],x):
          if(Ans[i][0] != x[0]):
            return False
          else:
            I.remove(x)
            break
  return True
  
#input
N = int(input())
A = input().split()
Initial = A.copy()
B = A.copy()

#バブルソート
count = 0
flag = True
while flag:
  flag = False
  j = N - 1
  while j >= 1:
    if greater(A[j-1],A[j]):
      v = A[j]
      A[j] = A[j-1]
      A[j-1] = v
      flag = True
      count += 1
    j -= 1
pprint(A)
print("Stable")

#選択ソート
count = 0
for i in range(N):
  j=i
  minj=i
  while j<= N-1:
    if greater(B[minj],B[j]):
      minj = j
    j += 1
  if i != minj:
    v=B[i]
    B[i]=B[minj]
    B[minj]=v
    count += 1
    
print(" ".join(map(str,B)))
if wasStable(B,Initial,N):
  ans = "Stable"
else:
  ans = "Not stable"
print(ans)

    

