def BubbleSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
  return cards

def SelectionSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
  return cards

class Card(object):
  """docstring for Card"""
  def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1])

num = int(raw_input())
inputs = raw_input().split()
cards = [Card(inputs[x]) for x in xrange(num)]

bubble_ans = ""
bubble_cards = BubbleSort(cards, num)
for x in bubble_cards:
  bubble_ans += x.mark + str(x.value) + " "
print bubble_ans.rstrip()
print "Stable"

select_ans = ""
select_cards = SelectionSort(cards, num)
for x in select_cards:
  select_ans += x.mark + str(x.value) + " "
print select_ans.rstrip()
flag = True
for x in xrange(num):
  if bubble_cards[x].mark != select_cards[x].mark:
    flag = False
    break
print "Stable" if flag else "Not stable" def BubbleSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
  return cards BubbleSort cards = lists[:] cards lists[:] lists : for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
   i xrange(num) xrange num for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
   j xrange(num - 1, i, -1) xrange num - 1 num 1 i -1 if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
   cards[j].value < cards[j - 1].value cards[j].value [j] cards j value cards[j - 1].value [j - 1] cards j - 1 j 1 value cards[j], cards[j - 1] = cards[j - 1], cards[j] cards[j] cards j cards[j - 1] cards j - 1 j 1 cards[j - 1] cards j - 1 j 1 cards[j] cards j return cards cards lists lists num num def SelectionSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
  return cards SelectionSort cards = lists[:] cards lists[:] lists : for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
   i xrange(num) xrange num minj = i minj i for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
     j xrange(i, num) xrange i num if cards[j].value < cards[minj].value:
        minj = j
     cards[j].value < cards[minj].value cards[j].value [j] cards j value cards[minj].value [minj] cards minj value minj = j minj j cards[i], cards[minj] = cards[minj], cards[i] cards[i] cards i cards[minj] cards minj cards[minj] cards minj cards[i] cards i return cards cards lists lists num num class Card(object):
  """docstring for Card"""
  def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1]) Card """docstring for Card""" def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1]) __init__ self.mark = arg[0] self.mark self mark arg[0] arg 0 self.value = int(arg[1]) self.value self value int(arg[1]) int arg[1] arg 1 self self arg arg object num = int(raw_input()) num int(raw_input()) int raw_input() raw_input inputs = raw_input().split() inputs raw_input().split() ().split () raw_input split Card(inputs[x]) for x in xrange(num) for x in xrange(num) x xrange(num) xrange num for x in xrange(num) Card(inputs[x]) Card inputs[x] inputs x cards = [Card(inputs[x]) for x in xrange(num)] cards [Card(inputs[x]) for x in xrange(num)] bubble_ans = "" bubble_ans "" bubble_cards = BubbleSort(cards, num) bubble_cards BubbleSort(cards, num) BubbleSort cards num for x in bubble_cards:
  bubble_ans += x.mark + str(x.value) + " " x bubble_cards bubble_ans += x.mark + str(x.value) + " " bubble_ans x.mark + str(x.value) + " " x.mark + str(x.value) x.mark x mark str(x.value) str x.value x value " " print bubble_ans.rstrip() bubble_ans.rstrip bubble_ans rstrip print select_ans = "" select_ans "" select_cards = SelectionSort(cards, num) select_cards SelectionSort(cards, num) SelectionSort cards num for x in select_cards:
  select_ans += x.mark + str(x.value) + " " x select_cards select_ans += x.mark + str(x.value) + " " select_ans x.mark + str(x.value) + " " x.mark + str(x.value) x.mark x mark str(x.value) str x.value x value " " print select_ans.rstrip() select_ans.rstrip select_ans rstrip flag = True flag True for x in xrange(num):
  if bubble_cards[x].mark != select_cards[x].mark:
    flag = False
    break x xrange(num) xrange num if bubble_cards[x].mark != select_cards[x].mark:
    flag = False
    break bubble_cards[x].mark != select_cards[x].mark bubble_cards[x].mark [x] bubble_cards x mark select_cards[x].mark [x] select_cards x mark flag = False flag False break print "Stable" if flag else "Not stable" flag "Stable" "Not stable" cards = [Card(inputs[x]) for x in xrange(num)] [Card(inputs[x]) for x in xrange(num)] cards bubble_cards = BubbleSort(cards, num) BubbleSort(cards, num) bubble_cards select_ans = "" "" select_ans bubble_ans += x.mark + str(x.value) + " " x.mark + str(x.value) + " " bubble_ans flag = True True flag def SelectionSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
  return cards def SelectionSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
  return cards SelectionSort bubble_ans = "" "" bubble_ans select_cards = SelectionSort(cards, num) SelectionSort(cards, num) select_cards inputs = raw_input().split() raw_input().split() inputs class Card(object):
  """docstring for Card"""
  def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1]) class Card(object):
  """docstring for Card"""
  def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1]) Card flag = False False flag def BubbleSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
  return cards def BubbleSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
  return cards BubbleSort select_ans += x.mark + str(x.value) + " " x.mark + str(x.value) + " " select_ans num = int(raw_input()) int(raw_input()) num
def BubbleSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    for j in xrange(num - 1, i, -1):
      if cards[j].value < cards[j - 1].value:
        cards[j], cards[j - 1] = cards[j - 1], cards[j]
  return cards

def SelectionSort(lists, num):
  cards = lists[:]
  for i in xrange(num):
    minj = i
    for j in xrange(i, num):
      if cards[j].value < cards[minj].value:
        minj = j
    cards[i], cards[minj] = cards[minj], cards[i]
  return cards

class Card(object):
  """docstring for Card"""
  def __init__(self, arg):
    self.mark = arg[0]
    self.value = int(arg[1])

num = int(raw_input())
inputs = raw_input().split()
cards = [Card(inputs[x]) for x in xrange(num)]

bubble_ans = ""
bubble_cards = BubbleSort(cards, num)
for x in bubble_cards:
  bubble_ans += x.mark + str(x.value) + " "
print bubble_ans.rstrip()
print "Stable"

select_ans = ""
select_cards = SelectionSort(cards, num)
for x in select_cards:
  select_ans += x.mark + str(x.value) + " "
print select_ans.rstrip()
flag = True
for x in xrange(num):
  if bubble_cards[x].mark != select_cards[x].mark:
    flag = False
    break
print "Stable" if flag else "Not stable"