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)

#include<stdio.h>

#define MAX 100000
#define INFTY 1000000002

typedef struct node{
  int key;
  char type;
}Card;

int n;
Card trump1[MAX],trump2[MAX];
Card L[MAX/2+2],R[MAX/2+2];

void Merge(int l,int m,int r){
  int i,j,k;
  int na = m - l;
  int nb = r - m;

  for(i = 0; i < na; i++)L[i] = trump1[l + i];
  for(j = 0; j < nb; j++)R[j] = trump1[m + j];
  L[na].key = R[nb].key = INFTY;
  i = j = 0;
  for(k = l; k < r; k++){
    if(L[i].key <= R[j].key){
      trump1[k] = L[i] ;
      i++;
    }
    else {
      trump1[k] = R[j];
      j++;
    }
  }
}

void MergeSort(int l,int r){
  int m;
  if(l + 1 < r){
    m = (l + r)/2;
    MergeSort(l,m);
    MergeSort(m,r);
    Merge(l,m,r);
  }
}
int Partition(int p,int r){
  int i,j;
  Card x,v;
  
  x = trump2[r];
  i = p - 1;
  for(j = p; j <= r - 1; j++){
    if(trump2[j].key <= x.key){
      i = i + 1;
      v = trump2[i];
      trump2[i] = trump2[j];
      trump2[j] = v;
    }
  }
  v = trump2[i+1];
  trump2[i+1] = trump2[r];
  trump2[r] = v;

  return i+1;
}

void QuickSort(int p,int r){
  int h;
  if(p < r){
    h = Partition(p,r);
    QuickSort(p,h-1);
    QuickSort(h+1,r);
  }
}

void isStable(){
  int i,stable = 0;
  for(i = 0; i < n; i++){
    if(trump1[i].type == trump2[i].type){
      stable = 1;
    }
    else {
      stable = 0;
      break;
    }
  }
  
  if(stable == 1)printf("Stable\n");
  else printf("Not stable\n");
}

int main(){
  int i,key;
  char type;
  
  scanf("%d",&n);
  for(i = 0; i < n; i++){
    scanf("%s %d",&type,&key);
    trump1[i].key = trump2[i].key = key;
    trump1[i].type = trump2[i].type = type;
  }
  //if(n <= 2)MergeSort(0,n-1);
  MergeSort(0,n);
  QuickSort(0,n - 1);
  isStable();

  for(i = 0; i < n; i++)printf("%c %d\n",trump2[i].type,trump2[i].key);
  //for(i = 0; i < n; i++)printf("%c %d\n",trump1[i].type,trump1[i].key);
  
  return 0;
}