# Card
class Card
  attr_reader :suit, :value

  def initialize(suit, value)
    @suit = suit
    @value = value
  end
end

def bubble_sort a
  n = a.size
  0.upto(n - 1) do |i|
    (n - 1).downto(i + 1) do |j|
      if a[j].value < a[j - 1].value
        a[j], a[j - 1] = a[j - 1], a[j]
      end
    end
  end
  a
end

def selection_sort a
  n = a.size
  0.upto(n - 1) do |i|
    mini = i
    i.upto(n - 1) do |j|
      mini = j if a[j].value < a[mini].value
    end
    unless i == mini
      a[i], a[mini] = a[mini], a[i]
    end
  end
  a
end

n = gets.chop.to_i
a = []
gets.chop.split.each do |card|
  a << Card.new(card[0], card[1])
end

b = bubble_sort(a.dup).map { |card| card.suit + card.value.to_s }
s = selection_sort(a.dup).map { |card| card.suit + card.value.to_s }

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