class Card
	attr_reader :suit
	attr_reader :no
	attr_reader :name
	def initialize(n)
		@name = n
		@suit = n[0]
		@no = n[1].to_i
	end

	def Search(n)
		return self if n == @no
	end
end

def Swap(a, b)
	return b,a
end

def PrintArray(d)
	d.length.times { |i|
		print " " unless i == 0
		print "#{d[i].name}"
	}
	puts
end

def Stable(a, b)
	stable = true
	(1..9).each { |i|
		bt = b.each_with_object(i).map(&:Search).compact
		next if bt.length < 1
		a.each_with_object(i).map(&:Search).compact.each_with_index { |at, j|
			if at.name != bt[j].name
				stable = false
				break
			end
		}
		break if not stable
	}
	print "Not " if not stable
	puts "Stable"
end

def BubbleSort(d_o)
	d = Marshal.load(Marshal.dump(d_o))
	(d.length-1).times { |i|
		(d.length-1).step(i+1, -1) { |j|
			d[j],d[j-1] = Swap(d[j],d[j-1]) if d[j-1].no > d[j].no
		}
	}
	PrintArray(d)
	Stable(d, d_o)
end

def SelectionSort(d_o)
	d = Marshal.load(Marshal.dump(d_o))
	d.length.times { |i|
		min = i
		(i...d.length).each_with_index { |j|
			min = j if d[min].no > d[j].no
		}
		d[i],d[min] = Swap(d[i],d[min]) if i != min
	}
	PrintArray(d)
	Stable(d, d_o)
end

gets
d = Array.new
gets.chomp.split(" ").each { |c| d << Card.new(c) }
BubbleSort(d)
SelectionSort(d)