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: Time Limit Exceeded)

#include <stdio.h>
#define MAX 100000
#define MAX2 1000
 
int partition( int *A, int *Order, int p, int r );
 
int Quicksort( int *A, int *Order, int p, int r );

int IsStable(int Arr[MAX], int Order[MAX], int num );

int Store(int Arr[MAX], int Order[MAX], int Storage[MAX2][MAX2], int num );

int Judge_Include(int Storage[MAX2][MAX2], int num_for_store, int a);

void Insert(int Arr[MAX], int Order[MAX], int i, int Storage[MAX2][MAX2], int num_for_Store, int num ) ;


int main(void)
{
    int Arr[MAX] = {};
    int Original[MAX] = {};
    int Order[MAX] = {};
    int Arr2[MAX] = {};
    char str[MAX][2] = {};
    int i = 0;
    int flag = 0;
    int order = 0;
      
    scanf("%d\n", &order);
      
    for ( i = 0; i < order; i++ ) {
        scanf("%s", str[i]);
        scanf("%d", &Arr[i]);
        Original[i] = Arr[i];
        Order[i] = i;
    }
     
     
     
    Quicksort(Arr, Order, 0, order-1);
    for ( i = 0; i < order; i++ ) {
        Arr2[i] = Arr[Order[i]];
    }

    flag = IsStable( Original, Order, order );
    if ( flag < 0 ) {
        printf("Not stable\n");
    } else {
        printf("Stable\n");
    }
     
    for ( i = 0; i < order - 1 ; i++ ) {
        printf("%c %d\n", str[Order[i]][0], Arr[Order[i]]);
    }
    printf("%c %d\n", str[Order[i]][0], Arr[Order[i]]);
    return 0;
}
  
int partition( int *A, int *Order, int p, int r ) 
{
    int x = 0;
    int i = 0;
    int j = 0;
    int tmp = 0;
    static int kaisu = 0;
      
    x = A[Order[r]];
    i = p - 1;
    for ( j = p; j < r; j++ ) {
        if ( A[Order[j]] <= x ) {
            i = i+1;
            tmp = Order[i];
            Order[i] = Order[j];
            Order[j] = tmp;
        }
    }
      
    tmp = Order[r];
    Order[r] = Order[i+1];
    Order[i+1] = tmp;
   // printf("回数：%d p:%d r:%d\n", kaisu, p, r);
   /* for ( j = p; j < r + 1; j++ ) {
        printf("%d ", Order[j]);
    }
    kaisu++;
    printf("\n");*/
    return i+1;
}
 
int Quicksort( int *A, int *Order, int p, int r )
{
    int q = 0;
    static int kaisu = 0;
    if ( p < r ) {
        q = partition( A, Order, p, r );
        //printf("回数：%d partition：%d p:%d r:%d\n\n", kaisu, q, p, r);
        kaisu++;
        Quicksort(A, Order, p, q-1);
        Quicksort(A, Order, q+1, r);
    }
    return 0;
}
 
 

int IsStable(int Arr[MAX], int Order[MAX], int num ) 
{
	int i = 0;
	int a = 0;
	int b = 0;
	int Storage[MAX2][MAX2] = {};
	int num_for_store = 0;
	
	for ( a = 0; a < MAX2; a++ ) {
	    for ( b = 0; b < MAX2; b++ ) {
	        Storage[a][b] = -1;
	    }
	}
	
	num_for_store = Store(Arr, Order, Storage, num);
	
	
	for ( i = 0; i < num_for_store; i++ ) {
		for ( a = 0; Storage[i][a] >= 0; a++ ) {
			for ( b = a + 1; Storage[i][b] >= 0; b++ ) {
				if ( Order[Storage[i][a]] > Order[Storage[i][b]] ) {
					return -1;
				}
			}
		}
	}
	
	return 0;
}

int Store(int Arr[MAX], int Order[MAX], int Storage[MAX2][MAX2], int num )
{
	int i = 0;
	int j = 0;
	int a = 0;
	int b = 0;
	int num_for_store = 0;
	
	for ( i = 0; i < num; i++ ) {
		if ( Judge_Include(Storage, num_for_store, i) < 0 ) {
			continue;
		}
		
		for ( j = i+1; j < num; j++ ) {
			if ( Judge_Include(Storage, num_for_store, j) < 0 ) {
				continue;
			}
			
			if ( Arr[Order[i]] != Arr[Order[j]] ) {
				continue;
			}
			
			Insert(Arr, Order, i, Storage, num_for_store ,num);
			num_for_store++;
		}
	}
	
	return num_for_store;
}

int Judge_Include(int Storage[MAX2][MAX2], int num_for_store, int a)
{
	int i = 0;
	int j = 0;
	
	for ( i = 0; i < num_for_store; i++ ) {
		for ( j = 0; Storage[i][j] >= 0; j++ ) {
			if ( Storage[i][j] == a ) {
				return -1;
			}
		}
	}
	
	return 0;
}

void Insert(int Arr[MAX], int Order[MAX], int i, int Storage[MAX2][MAX2], int num_for_store, int num ) 
{
	int j = 0;
	int k = 0;
	
	for ( j = 0; j < num; j++ ) {
		if ( Arr[Order[j]] == Arr[Order[i]] ) {
			Storage[num_for_store][k] = j;
			k++;
		}
	}
}