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

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Card struct {
	mark  string
	value int
}

func (self Card) Equal(other Card) bool {
	return self.mark == other.mark && self.value == other.value
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	cards, err := nextCards(n, sc)
	if err != nil {
		panic(err)
	}
	copied := make([]Card, len(cards))
	copy(copied, cards)
	quickSort(cards)
	sortedByMerge, _ := mergeSort(copied)
	if isEqualCards(cards, sortedByMerge) {
		fmt.Println("Stable")
	} else {
		fmt.Println("Not stable")
	}
	for _, card := range cards {
		fmt.Printf("%s %d\n", card.mark, card.value)
	}
}

func quickSort(cards []Card) {
	if len(cards) > 1 {
		q := partition(cards, 0, len(cards)-1)
		quickSort(cards[:q])
		quickSort(cards[q+1:])
	}
}

func partition(cards []Card, p int, r int) int {
	// fmt.Printf("before: %v\n", cards)
	k := p - 1
	v := cards[r].value
	for i := p; i < r; i++ {
		if cards[i].value <= v {
			k++
			cards[i], cards[k] = cards[k], cards[i]
		}
	}
	k++
	cards[r], cards[k] = cards[k], cards[r]
	// fmt.Printf("after: %v\n", cards)
	return k
}

func mergeSort(elems []Card) ([]Card, int) {
	length := len(elems)
	if length == 1 {
		return elems, 0
	}
	mid := length / 2
	left, countL := mergeSort(elems[:mid])
	right, countR := mergeSort(elems[mid:])
	sorted, countS := merge(left, right)
	return sorted, (countL + countR + countS)
}

func merge(xs, ys []Card) ([]Card, int) {
	sorted := make([]Card, 0)
	count := 0
	for len(xs) > 0 && len(ys) > 0 {
		if xs[0].value <= ys[0].value {
			sorted = append(sorted, xs[0])
			xs = xs[1:]
		} else {
			sorted = append(sorted, ys[0])
			ys = ys[1:]
		}
		count++
	}
	if len(xs) > 0 {
		sorted = append(sorted, xs...)
		count += len(xs)
	} else if len(ys) > 0 {
		sorted = append(sorted, ys...)
		count += len(ys)
	}
	return sorted, count
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextCard(sc *bufio.Scanner) (Card, error) {
	mark := nextText(sc)
	value, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	return Card{mark, value}, nil
}

func nextCards(n int, sc *bufio.Scanner) ([]Card, error) {
	ns := make([]Card, n)
	for i, _ := range ns {
		x, err := nextCard(sc)
		if err != nil {
			return nil, err
		}
		ns[i] = x
	}
	return ns, nil
}

func isEqualCards(x []Card, y []Card) bool {
	if len(x) != len(y) {
		return false
	}
	for i := 0; i < len(x); i++ {
		if !x[i].Equal(y[i]) {
			return false
		}
	}
	return true
}