class Card
	attr_accessor :suit, :value
	
	def initialize(str)
		@suit = str[0]
		@value = str[1].to_i
	end

	def to_s
		"#@suit#@value"
	end

	def self.bubble_sort(c)
		c = c.dup
		n = c.size
		0.upto(n-1){|i|
			(n-1).downto(i){|j|
				if c[j].value < c[j-1].value
					c[j], c[j-1] = c[j-1], c[j]
				end
			}
		}
		c
	end

	def self.selection_sort(c)
		c = c.dup
		n = c.size
		0.upto(n-1){|i|
			minj = i
			i.upto(n-1){|j|
				if c[j].value < c[minj].value
					minj = j
				end
			}
			c[i], c[minj] = c[minj], c[i]
		}
		c
	end
end

gets
cards = gets.split.map{|s| Card.new s }

cards1 = Card.bubble_sort(cards)
cards2 = Card.selection_sort(cards)

puts cards1*' '
puts 'Stable'
puts cards2*' '
puts cards1 == cards2 ? 'Stable' : 'Not Stable'