def bubble(n, nums, strings)
  n.times do |i|
    a = n - 1
    i += 1
    while true do
      if a < i
        break
      end
      if nums[a] < nums[a - 1]
        tmp = nums[a]
        nums[a] = nums[a - 1]
        nums[a- 1] = tmp
        tmp_s = strings[a]
        strings[a] = strings[a - 1]
        strings[a- 1] = tmp_s
        a -= 1
      else
        a -= 1
      end
    end
  end
  return nums, strings
end

def selection(n, nums, strings)
  n.times do |nn|
    s = n - 1 - nn
    nm = nn + 1
    min = nums[nn]
    hoge = -111
    s.times do
      if min > nums[nm]
        hoge = nm
        min = nums[nm]
      end
      nm += 1
    end
    #入れ替え作業
    if hoge > 0
      tmp = nums[nn]
      nums[nn] = nums[hoge]
      nums[hoge] = tmp
      tmp_s = strings[nn]
      strings[nn] = strings[hoge]
      strings[hoge] = tmp_s
    end
  end
  return nums, strings
end

n = gets.chomp.to_i
tramps = gets.chomp.split(" ")
nums = tramps.map{|t| t.split("")[1].to_i}
strings = tramps.map{|t| t.split("")[0]}

nums_s = tramps.map{|t| t.split("")[1].to_i}
strings_s = tramps.map{|t| t.split("")[0]}

new_bubble_nums, new_bubble_strings = bubble(n, nums, strings)
new_selection_nums, new_selection_strings = selection(n, nums_s, strings_s)

bubble_result = []
selection_result = []

n.times do |a|
  bubble_result << new_bubble_strings[a] + new_bubble_nums[a].to_s
end
n.times do |b|
  selection_result << new_selection_strings[b] + new_selection_nums[b].to_s
end

puts bubble_result.join(" ")
puts 'Stable'

puts selection_result.join(" ")

if new_bubble_strings == new_selection_strings
  puts 'Stable'
else
  puts 'Not stable'
end

