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 Rust (judge verdict: Accepted)

use std::fmt;
use std::io::*;
use std::str::FromStr;

//https://qiita.com/tubo28/items/e6076e9040da57368845
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

//newtype pattern
//https://ja.stackoverflow.com/questions/34639/rust-rcfoo-%E3%81%AB%E3%83%88%E3%83%AC%E3%82%A4%E3%83%88-stdopadd-%E3%82%92%E5%AE%9F%E8%A3%85%E3%81%95%E3%81%9B%E3%81%9F%E3%81%84
#[derive(Debug, Clone, Copy)]
struct Card(char, u32);

//https://doc.rust-jp.rs/rust-by-example-ja/hello/print/print_display.html
impl fmt::Display for Card {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {}", self.0, self.1)
    }
}

fn quick_sort(a: &mut Vec<Card>, p: usize, r: usize) {
    if p < r {
        //pからrまでの範囲でpartition、基準値のインデックスをもらう
        let q = partition(a, p, r);
        if q != 0 {
            //左側から基準値までを再帰quick_sort
            quick_sort(a, p, q - 1);
            //基準値から右側を再帰quick_sort
            quick_sort(a, q + 1, r);
        }
    }
}

//a:対象配列 p:分割する左端のインデックス r:分割する右端のインデックス、基準値のインデックス
//基準値のインデックスを返す
fn partition(a: &mut Vec<Card>, p: usize, r: usize) -> usize {
    //基準値設定
    let x = a[r].1;
    //左側に分割するインデックス
    let mut i = p;
    //左端から右端までを基準値と比較し、基準値以下であれば左側に移動する
    //このとき基準値以上の値が右側に移動する
    for j in p..r {
        if a[j].1 <= x {
            //基準値以下と基準値より大きい値をswap
            a.swap(i, j);
            i = i + 1;
        }
    }
    //基準値を境界に挿入する
    a.swap(i, r);
    i
}

fn mergesort(a: &mut Vec<Card>, left: usize, right: usize) -> usize {
    //両端のインデックスが隣り合う。2要素になるまで分割する。
    //分割の順番はDFSの順番
    if right - left > 1 {
        //if left + 1 < right {
        let mid = (left + right) / 2;
        let count =
            //左側を分割
            mergesort(a, left, mid)
            //右側を分割
            + mergesort(a, mid, right)
            //統合
            + merge(a, left, mid, right);
        count
    } else {
        0
    }
}

fn merge(a: &mut Vec<Card>, left: usize, mid: usize, right: usize) -> usize {
    let mut l: Vec<Card> = a[left..mid].iter().cloned().collect();
    let mut r: Vec<Card> = a[mid..right].iter().cloned().collect();

    //番兵を初期設定時に加える方法が不明。。
    l.push(Card('X', std::u32::MAX));
    r.push(Card('X', std::u32::MAX));

    let mut i = 0;
    let mut j = 0;
    let mut count = 0;

    for k in left..right {
        count += 1;
        if l[i].1 <= r[j].1 {
            a[k] = l[i];
            i += 1;
        } else {
            a[k] = r[j];
            j += 1;
        }
    }
    count
}

fn main() {
    let n: usize = read();
    let mut a: Vec<Card> = (0..n).map(|_| Card(read::<String>().chars().next().unwrap(), read())).collect();
    let mut b = a.clone();

    quick_sort(&mut a, 0, n - 1);
    mergesort(&mut b, 0, n);

    //stable確認
    let mut stable = true;
    for i in 0..n {
        if a[i].0 != b[i].0 {
            stable = false;
        }
    }

    println!("{}", if stable { "Stable" } else { "Not stable" });

    for t in a.iter() {
        println!("{}", *t);
    }
}