class Card
  include Comparable

  attr_reader :suit, :value

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

  def <=>(other)
    @value - other.value
  end

  def ==(other)
    @suit == other.suit && @value == other.value
  end

  def to_s
    "#{@suit}#{@value}"
  end
end

def bubble_sort(arr)
  n = arr.size

  n.times do |i|
    (n - 1).downto(i + 1) do |j|
      if arr[j] < arr[j - 1]
        arr[j - 1], arr[j] = arr[j], arr[j - 1]
      end
    end
  end
end

def selection_sort(arr)
  n = arr.size

  n.times do |i|
    min_pos = i

    i.upto(n - 1) do |j|
      if arr[j] < arr[min_pos]
        min_pos = j
      end
    end

    arr[i], arr[min_pos] = arr[min_pos], arr[i]
  end
end

def is_stable(arr1, arr2)
  return false unless arr1.size == arr2.size

  arr1.size.times do |i|
    return false unless arr1[i] == arr2[i]
  end

  return true
end

n = gets.to_i

arr1 = gets.split.map do |str|
  suit = str[0].to_sym
  value = str[1].to_i

  Card.new(suit, value)
end

arr2 = arr1.dup

bubble_sort(arr1)
selection_sort(arr2)

puts arr1.join(' ')
puts 'Stable'

puts arr2.join(' ')
puts is_stable(arr1, arr2) ? 'Stable' : 'Not stable'