def bubble_sort(array)
  a = array.dup
  (0...a.size).each do |i|
    (i + 1...a.size).reverse_each do |j|
      if a[j][1] < a[j - 1][1]
        a[j], a[j - 1] = a[j - 1], a[j]
      end
    end
  end
  a
end

def selection_sort(array)
  a = array.dup
  (0...a.size).each do |i|
    mini = i
    (i...a.size).each do |j|
      mini = j if a[j][1] < a[mini][1]
    end
    a[i], a[mini] = a[mini], a[i]
  end
  a
end

n = gets.to_i
s = gets.split
b = bubble_sort(s)
s = selection_sort(s)

puts b.join(' ')
puts 'Stable'
puts s.join(' ')
puts s == b ? 'Stable' : 'Not stable'