Problem:
Quick Sort

Let's arrange a deck of cards. Your task is to sort totally n cards. A card consists of a part of a suit (S, H, C or D) and an number. Write a program which sorts such cards based on the following pseudocode:

Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4 do if A[j] <= x
5 then i = i+1
6 exchange A[i] and A[j]
7 exchange A[i+1] and A[r]
8 return i+1

Quicksort(A, p, r)
1 if p < r
2 then q = Partition(A, p, r)
3 run Quicksort(A, p, q-1)
4 run Quicksort(A, q+1, r)

Here, A is an array which represents a deck of cards and comparison operations are performed based on the numbers.

Your program should also report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input

The first line contains an integer n, the number of cards.

n cards are given in the following lines. Each card is given in a line and represented by a pair of a character and an integer separated by a single space.

Output

In the first line, print the stability ("Stable" or "Not stable") of this output.

In the following lines, print the arranged cards in the same manner of that of the input.

Constraints

1 ≤ n ≤ 100,000

1 ≤ the number of a card ≤ 109

There are no identical card in the input

Sample Input 1

6
D 3
H 2
D 1
S 3
D 2
C 1

Sample Output 1

Not stable
D 1
C 1
D 2
H 2
D 3
S 3

Sample Input 2

2
S 1
H 1

Sample Output 2

Stable
S 1
H 1

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project
{
	class Card
	{
		public char mark;
		public int num;
		public Card() { }
		public Card(char mark, int num)
		{
			this.mark = mark;
			this.num = num;
		}
	}

	class MainClass
	{
		public static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			Card[] cards = new Card[n];
			Card[] ccards = new Card[n];
			for (int i = 0; i < n; i++)
			{
				string[] s = Console.ReadLine().Split(' ');
				cards[i] = new Card(s[0][0], int.Parse(s[1]));
				ccards[i] = new Card(s[0][0], int.Parse(s[1]));
			}
			MergeSort(ccards, 0, n);
			var sb = new StringBuilder();
			for (int i = 0; i < n; i++)
			{
				sb.Append(ccards[i].mark);
				sb.Append(":");
				sb.Append(ccards[i].num);
				if (i != n - 1)
					sb.Append(" ");
			}
			string s1 = sb.ToString();
			QuickSort(cards, 0, n - 1);
			sb = new StringBuilder();
			for (int i = 0; i < n; i++)
			{
				sb.Append(cards[i].mark);
				sb.Append(":");
				sb.Append(cards[i].num);
				if (i != n - 1)
					sb.Append(" ");
			}
			string s2 = sb.ToString();
			if (s1 == s2)
				Console.WriteLine("Stable");
			else
				Console.WriteLine("Not stable");
			foreach (Card x in cards)
			{
				Console.WriteLine("{0} {1}", x.mark, x.num);
			}
		}

		public static int Partition(Card[] c, int p, int r)
		{
			Card tmp = new Card();
			int x = c[r].num;
			int i = p - 1;
			for (int j = p; j < r; j++)
			{
				if (c[j].num <= x)
				{
					i++;
					tmp.mark = c[i].mark;
					tmp.num = c[i].num;
					c[i].mark = c[j].mark;
					c[i].num = c[j].num;
					c[j].mark = tmp.mark;
					c[j].num = tmp.num;
				}
			}
			tmp.mark = c[i + 1].mark;
			tmp.num = c[i + 1].num;
			c[i + 1].mark = c[r].mark;
			c[i + 1].num = c[r].num;
			c[r].mark = tmp.mark;
			c[r].num = tmp.num;
			return i + 1;
		}

		public static void QuickSort(Card[] c, int p, int r)
		{
			if (p < r)
			{
				int q = Partition(c, p, r);
				QuickSort(c, p, q - 1);
				QuickSort(c, q + 1, r);
			}
		}

		public static void MergeSort(Card[] a, int left, int right)
		{
			if (left + 1 < right)
			{
				int mid = (left + right) / 2;
				MergeSort(a, left, mid);
				MergeSort(a, mid, right);
				Merge(a, left, mid, right);
			}
		}
		public static void Merge(Card[] a, int left, int mid, int right)
		{
			int n1 = mid - left;
			int n2 = right - mid;
			Card[] L = new Card[n1 + 1];
			Card[] R = new Card[n2 + 1];
			for (int l = 0; l < n1; l++)
			{
				L[l] = new Card(a[left + l].mark, a[left + l].num);
			}
			for (int l = 0; l < n2; l++)
			{
				R[l] = new Card(a[mid + l].mark, a[mid + l].num);
			}
			L[n1] = new Card(' ', int.MaxValue);
			R[n2] = new Card(' ', int.MaxValue);
			int i = 0, j = 0;
			for (int k = left; k < right; k++)
			{
				if (L[i].num <= R[j].num)
				{
					a[k].mark = L[i].mark;
					a[k].num = L[i].num;
					i++;
				}
				else
				{
					a[k].mark = R[j].mark;
					a[k].num = R[j].num;
					j++;
				}
			}
		}
	}
}