# ALDS1_2_C Stable Sort
n = gets.chomp.to_i
a = gets.chomp.split(" ")
def bubbleSort(c, n)
  a = c.dup
  (0..n-1).each do |i|
    (n-1).downto(i) do |j|
      if a[j][1].to_i < a[j-1][1].to_i
        a[j], a[j-1] = a[j-1], a[j]
      end
    end
  end
  return a
end

def selectionSort(c, n)
  a = c.dup
  (0..n-1).each do |i|
    minj = i
    (i..n-1).each do |j|
      if a[j][1].to_i < a[minj][1].to_i then
        minj = j
      end
    end
    a[i], a[minj] = a[minj], a[i]
  end
  return a
end
bubble = bubbleSort(a, n)
selection = selectionSort(a, n)
puts bubble.join(" ")
puts "Stable"
puts selection.join(" ")
if bubble == selection then
  puts "Stable"
else
  puts "Unstable"
end