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)

/**
  * Quick Sort
  *
  */

#include <stdio.h>
#include <string.h>
#define MAX 100000

/* structrue */
struct Element {
  char id; //?????????stable?????????????¢???????????????????id
  int value;
};
typedef struct Element Card; //?§???????'Element'???'Card'??¨????????£?¨?

/* prototype */
int partition(Card*, int, int);
void quickSort(Card*, int, int ); //??????A???sort????????????????????§???return???void
void insertionSort(Card*, int); //stable?¢?????????????????????\?????????
int checkStable(Card*, Card*, int);
void swap(Card*, int, int);

int main(){
  int i,n,pivot;
  
  /* input the number of A[] */
  scanf("%d",&n);

  Card A[n]; //for quickSort
  Card B[n]; //for insertionSort

  /* input datas */
  for(i=0;i<n;i++){
    int value;
    char str[3];
    scanf("%s %d", str, &value);
    A[i].id = B[i].id = str[0];
    A[i].value = B[i].value = value;
  }

//  for(i=0;i<n;i++){
//    A[i].id = i;
//    B[i].id = i;
//    scanf("%d",&A[i].value);
//    B[i].value = A[i].value;
//  }
  
  quickSort(A, 0, n-1);
  
  insertionSort(B, n);
//  printf("%d\n",checkStable(A, B, n));
  
  /* printing */
  if(checkStable(A, B, n)){
    printf("Stable\n");
  }else{
    printf("Not stable\n");
  }
//  for(i=0;i<n;i++){
//    printf("%d ", A[i].value);
//  }
//  printf("\n");
//  for(i=0;i<n;i++){
//    printf("%d ", B[i].value);
//  }
//  printf("\n");

  for(i=0;i<n;i++){
    printf("%c %d\n", A[i].id, A[i].value);
  }

  return 0;
}

int partition(Card *A, int p, int r) {
  int i,j,x;

  x = A[r].value; //key.????????¨????¬?????????????????????¨?????????
  i = p-1;  //'<x'??????
  /* 'x<'?????????????????????j????????¨????????°?????? */
  for(j=p;j<r;j++) { //'r'???key????????¢?´¢?????????
    if(A[j].value<=x){ //A[j].value???'<x'????????????????????\??????
      i++;
      swap(A, i, j);
    }
    //else?????´????????????'x<'???????????????????????????j++??§???????????????????????????????????????????¬????for??????
  }

  /* key(A[r])???'<x'????????????????????\?????? */
  i++; //'<x'???????????????????????????
  swap(A, i, r);
//  swap = A[i].value;
//  A[i].value = A[r].value;
//  A[r].value = swap;
  
  return i;
}

void quickSort(Card *A, int p, int r) {
  int pivot;
  if(p<r){ //???????????????????´???°?????§????§?????????????partition(A,0,0)?????????????????¨,for(j=p;j<r;j++)????????§???????????£???,p(=r)???????????¨p(=r)??????????????\???????????????p(=r)??????????????°??£???????????????????????????if(p==r)??§???????????????????????????.
    pivot = partition(A, p, r);
    quickSort(A, p, pivot-1); //'<x'???????????????????????°(???????????????)
    quickSort(A, pivot+1, r); //'x<'???????????????????????°(???????????????)
  }

//  for(i=0;i<n-1;i++){
//    if(i==pivot){
//      printf("[%d] ",A[i].value);
//    }else{
//      printf("%d ",A[i].value);
//    }
//  }//??????????????§i=n-1(????????????)
//  printf("%d\n",A[i].value); //??????
}

void insertionSort(Card *B, int n){
  int i,j,key_v;
  char key_c[3];

  for(i=1;i<n;i++){
    key_v = B[i].value;
    key_c[0] = B[i].id;
    
    //????????????key???????°????????????????????????°??????
    j = i-1; //?§???????key????????????
    while(j>=0 && B[j].value>key_v) {
      B[j+1].value = B[j].value; //key????????????'x<key'???????´?x???????´?
      B[j+1].id = B[j].id;

      j--;
    }
    B[j+1].value = key_v;
    B[j+1].id = key_c[0];
  }
}

int checkStable(Card *A, Card *B, int n){
  int i=0;
  while(i<n && A[i].id==B[i].id){
    i++;
  }
  
  if(i==n){
    return 1;
  }else{
    return 0;
  }
}

void swap(Card* ary, int i, int j){
  int swap_v;
  char swap_c[3];

  //value
  swap_v = ary[i].value;
  ary[i].value = ary[j].value;
  ary[j].value = swap_v;

  //id
  swap_c[0] = ary[i].id;
  ary[i].id = ary[j].id;
  ary[j].id = swap_c[0];
}