'''
5
H4 C9 S4 D2 C3
'''
N = int(input())
C1 = list(map(str, input().split()))
C2 = [None] * N

for i in range(N):
  C2[i] = C1[i]

def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort

def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))

def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable")

main()
 '''
5
H4 C9 S4 D2 C3
''' N = int(input()) N int(input()) int input() input C1 = list(map(str, input().split())) C1 list(map(str, input().split())) list map(str, input().split()) map str input().split() ().split () input split C2 = [None] * N C2 [None] * N [None] None N for i in range(N):
  C2[i] = C1[i] i range(N) range N C2[i] = C1[i] C2[i] C2 i C1[i] C1 i def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort bubble for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

   i range(0, N-1, 1) range 0 N-1 N 1 1 for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

   j range(N-1, i, -1) range N-1 N 1 i -1 if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

   int(C[j-1][1]) > int(C[j][1]) int(C[j-1][1]) int C[j-1][1] [j-1] C j-1 j 1 1 int(C[j][1]) int C[j][1] [j] C j 1 C[j-1], C[j] = C[j], C[j-1] C[j-1] C j-1 j 1 C[j] C j C[j] C j C[j-1] C j-1 j 1 print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '')) print str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '') (C).replace('[', '').replace(']', '').replace(',', '').replace (C).replace('[', '').replace(']', '').replace(',', '') (C).replace('[', '').replace(']', '').replace (C).replace('[', '').replace(']', '') (C).replace('[', '').replace (C).replace('[', '') (C).replace (C) str C replace '[' '' replace ']' '' replace ',' '' replace '\'' '' print("Stable") print "Stable" C C N N def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '')) selection for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

   i range(N) range N minj = i minj i for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
     j range(i+1, N) range i+1 i 1 N if (int(C[minj][1]) > int(C[j][1])):
        minj = j
     int(C[minj][1]) > int(C[j][1]) int(C[minj][1]) int C[minj][1] [minj] C minj 1 int(C[j][1]) int C[j][1] [j] C j 1 minj = j minj j C[minj], C[i] = C[i], C[minj] C[minj] C minj C[i] C i C[i] C i C[minj] C minj print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '')) print str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '') (C).replace('[', '').replace(']', '').replace(',', '').replace (C).replace('[', '').replace(']', '').replace(',', '') (C).replace('[', '').replace(']', '').replace (C).replace('[', '').replace(']', '') (C).replace('[', '').replace (C).replace('[', '') (C).replace (C) str C replace '[' '' replace ']' '' replace ',' '' replace '\'' '' C C N N def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable") main bubble(C1, N) bubble C1 N selection(C2, N) selection C2 N if (C1 == C2):
    print("Stable")
  else:
    print("Not stable") C1 == C2 C1 C2 print("Stable") print "Stable" print("Not stable") print "Not stable" main() main C2 = [None] * N [None] * N C2 C1 = list(map(str, input().split())) list(map(str, input().split())) C1 def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort bubble def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '')) def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', '')) selection N = int(input()) int(input()) N def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable") def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable") main
'''
5
H4 C9 S4 D2 C3
'''
N = int(input())
C1 = list(map(str, input().split()))
C2 = [None] * N

for i in range(N):
  C2[i] = C1[i]

def bubble(C, N):
  #count = 0
  for i in range(0, N-1, 1):
    for j in range(N-1, i, -1):
      #print("j:{} C[j-1]:{} C[j]:{}".format(j, C[j-1], C[j]))
      if (int(C[j-1][1]) > int(C[j][1])):
        C[j-1], C[j] = C[j], C[j-1]
        #count += 1
        #print("Swapped. C:{}".format(C))

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))
  print("Stable") # because buddle sort is stable sort

def selection(C, N):
  for i in range(N):
    minj = i
    for j in range(i+1, N):
      if (int(C[minj][1]) > int(C[j][1])):
        minj = j
    C[minj], C[i] = C[i], C[minj]

  print(str(C).replace('[', '').replace(']', '').replace(',', '').replace('\'', ''))

def main():
  bubble(C1, N)
  selection(C2, N)

  if (C1 == C2):
    print("Stable")
  else:
    print("Not stable")

main()

