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 JAVA (judge verdict: Wrong Answer)

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Scanner;


class Main {
	private static	int array[];
	private static	int num;
	
	public static void main(String args[]) {
		//実行処理
		run();
	}
	
	//実質メイン
	public static void run(){ 
		Scanner scanner = new Scanner(System.in);
		num = scanner.nextInt();//要素数
		array = new int[num];
		int[] bubble = new int[num];
		//配列作成
		makeDeck(num,scanner);
		//ソート処理
		bubble = bubbleSort();
		quicksort(0, num - 1 );
		//出力
		if( Arrays.equals( bubble , array ) ) {
			System.out.println("Stable");
		}else {
			System.out.println("Not stable");
		}
		output();
		//後処理
		scanner.close();
	}
	
	//クイックソート
	public static void quicksort(int par, int rim){
		int q;
		if ( par < rim ){
			q = partition(par, rim);
			quicksort(par, q - 1);
			quicksort(q + 1, rim);
		}
	}
	
	//パーティション
	public static int partition(int par,int rim){
		int[] numArray = new int[num];
		int[] dig = new int[num];
		for(int i = 0; i < num; i ++){
			dig[i] = array[i] / 100;
			numArray[i] = array[i] % 100;
		}
		int work = 0;
		int sub = 0;
		int x = numArray[rim];
		int i = par - 1;
		
		//ソート処理
		for (int j = par ; j < rim ; j ++ ){
			if ( numArray[j] <= x ) {
				work = numArray[++i];
				numArray[i] = numArray[j];
				numArray[j] = work;
				sub = dig[i];
				dig[i] = dig[j];
				dig[j] = sub;
			}
		}
		//最後に入れ替え
		work = numArray[++i];
		numArray[i] = numArray[rim];
		numArray[rim] = work;
		sub = dig[i];
		dig[i] = dig[rim];
		dig[rim] = sub;
		for(int k = 0; k < num; k ++){
			array[k] = (dig[k] * 100)+  numArray[k];
		}
		return i;
	}
	
	//安定判断用バブルソート
	public static int[] bubbleSort(){ 
		int[] numArray = new int[num];
		int[] dig = new int[num];
		int[] out = new int[num];
		
		for(int i = 0; i < num; i ++)
		{
			dig[i] = array[i] / 100;
			numArray[i] = array[i] % 100;
		}
		//バブルソート処理
		for (int i = 0; i < num - 1; i++) {
				for (int j = num - 1; j > i; j--) {
					if (numArray[j - 1] > numArray[j]) {
						int work = numArray[j - 1];
						numArray[j - 1] = numArray[j];
						numArray[j] = work;
						int sub = dig[j - 1];
						dig[j - 1] = dig[j];
						dig[j] = sub;
					}
				}
			}
		for(int i = 0; i < num; i ++)
		{
			out[i] = (dig[i] * 100)+  numArray[i];
		}
		return out;
	}
	
	//山作り
	public static void makeDeck(int num, Scanner scanner){
		int count = 0;
		byte[] mark_num = new byte[100];
		byte[] trunp_byte = new byte[100];
		
		while( num > count ){
			String tranp = scanner.next();
			String num_trump = scanner.next();
			try {
				mark_num = tranp.getBytes("US-ASCII");
				trunp_byte = num_trump.getBytes("US-ASCII");
			} catch (Exception e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			//マーク数値化
			switch(getMark(mark_num[0]))
			{
			    case S:
			        array[count] = 100 + trunp_byte[0];
			        break;
			    case H:
			        array[count] = 200 + trunp_byte[0];
			        break;
			    case C:
			        array[count] = 300 + trunp_byte[0];
			        break;
			    case D:
			        array[count] = 400 + trunp_byte[0];
			        break;
			}
			count ++;
		}
	}
	
	//出力
	public static void output(){
		StringBuilder sb = new StringBuilder("");
		int mark;
		String trump_num = "";
		for (int i = 0; i < num; i++) {
			mark = array[i] / 100;
			byte[] sub = new byte[]{ (byte)(array[i] % 100) };
			
			try {
				trump_num = new String(sub, "US-ASCII");
			} catch (UnsupportedEncodingException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			//マークを判別
			switch( mark )
		    {
		        case 1:
					sb.append("S " + trump_num + "\n"); 
		            break;
		        case 2:
					sb.append("H " + trump_num + "\n"); 
		            break;
		        case 3:
					sb.append("C " + trump_num + "\n"); 
		            break;
		        case 4:
					sb.append("D " + trump_num + "\n"); 
		            break;
		    }
		}
		System.out.print(sb);
	}
	
    //enum宣言
	public static enum cardMarkNum
	{
        //マークをアスキーコードで紐づけ
		S(83),
		H(72),
		C(67),
		D(68);
		private final int mark;
		
		private cardMarkNum(final int mark) {
			this.mark = mark;
		}
		public int getInt(){
			return this.mark;
		}
	}
	public static cardMarkNum getMark(final int id) {
		cardMarkNum[] marks = cardMarkNum.values();
		for (cardMarkNum type : marks) {
			if (type.getInt() == id) {
				return type;
			}
		}
		return null;
	}
}