def bubble_sort(vec, num)
  num.times do |i|
    (num - 1).downto(i + 1) do |j|
      vec[j], vec[j - 1] = vec[j - 1], vec[j] if vec[j][1] < vec[j - 1][1]
    end
  end
end

def selection_sort(vec, num)
  num.times do |i|
    minj = i
    (i...num).each do |j|
      minj = j if vec[j][1] < vec[minj][1]
    end

    vec[i], vec[minj] = vec[minj], vec[i]
  end
end

n = gets.to_i
cards1 = gets.split.map { |x| x.split('') }
cards2 = cards1.dup

bubble_sort(cards1, n)
selection_sort(cards2, n)

n.times do |i|
  print ' ' unless i.zero?
  print cards1[i].join
end

puts
puts 'Stable'

n.times do |i|
  print ' ' unless i.zero?
  print cards2[i].join
end

puts

if cards1 == cards2
  puts 'Stable'
else
  puts 'Not stable'
end

