class Card

	include Comparable
	attr_accessor :suit, :number

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

	def <=>(other)
		self.number - other.number
	end

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

	def eql?(other)
		self.suit == other.suit && self.number == other.number
	end

	def self.create_sentinel
		Card.new("Z", Float::INFINITY)
	end

end

def partition(array, from, to)
	separator = array[to]
	i = from - 1
	(from...to).each do |j|
		if array[j] <= separator
			i = i + 1
			array[i], array[j] = array[j], array[i]
		end
	end
	array[i + 1], array[to] = array[to], array[i + 1]
	i + 1
end

def quick_sort(array, from, to)
	return unless from < to
	pivot = partition(array, from, to)
	quick_sort(array, from, pivot - 1)
	quick_sort(array, pivot + 1, to)
end

def merge_sort(array, left, right)
	return 0 unless left + 1 < right
	mid = (left + right) / 2
	compared_count  = merge_sort(array, left, mid)
	compared_count += merge_sort(array, mid, right)
	compared_count += merge(array, left, mid, right)
	compared_count
end

def merge(array, left, mid, right)
	left_array_size  = mid - left
	right_array_size = right - mid

	left_array  = []
	left_array_size.times { |i| left_array << array[left + i] }

	right_array = []
	right_array_size.times { |i| right_array << array[mid + i] }

	left_array  << Card.create_sentinel
	right_array << Card.create_sentinel

	compared_count = 0
	i = j = 0
	(left...right).each do |k|
		if left_array[i] <= right_array[j]
			array[k] = left_array[i]
			i += 1
		else
			array[k] = right_array[j]
			j += 1
		end
		compared_count += 1
	end
	compared_count
end

cards = []
array_size = STDIN.gets.to_i
array_size.times do
	suit, number = STDIN.gets.split[0..1]
	cards << Card.new(suit, number.to_i)
end

original_cards = cards.dup
quick_sort(cards, 0, array_size - 1)
merge_sort(original_cards, 0, array_size)
puts cards.eql?(original_cards) ? "Stable" : "Not stable"
cards.each do |c|
	puts c
end