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)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aoj.ALDS.Chapter6C
{
    class Card : IComparable<Card>
    {
        public string Suit { get; set; }
        public int Number { get; set; }

        public Card(string suit , int number)
        {
            Suit = suit;
            Number = number;
        }

        public int CompareTo(Card other)
        {
            return Number - other.Number;
        }

        public override string ToString()
        {
            return string.Format("{0} {1}", Suit, Number);
        }
    }
    class Program
    {
        /// <summary>
        /// array???????????????????????????????????????(??´?£??????????)???
        /// </summary>
        /// <param name="array"></param>
        /// <param name="left"></param>
        /// <param name="right">(????°????????´???????????????????)</param>
        public static void MergeSort<T>(List<T> array, int left, int right)
        where T : IComparable<T>
        {
            if (left + 1 < right)
            {
                int mid = (left + right) / 2;
                MergeSort(array, left, mid);
                MergeSort(array, mid, right);
                Merge(array, left, mid, right);
            }
        }

        private static void Merge<T>(List<T> array, int left, int mid, int right)
        where T : IComparable<T>
        {
            int n1 = mid - left;
            int n2 = right - mid;

            if (n1 < 0 || n2 < 0)
            {
                throw new System.ArgumentException("");
            }

            var leftArray = new List<T>();
            var rightArray = new List<T>();
            for (int i = 0; i < n1; i++)
            {
                leftArray.Add(array[left + i]);
            }
            for (int i = 0; i < n2; i++)
            {
                rightArray.Add(array[mid + i]);
            }

            int lc = 0;
            int rc = 0;
            for (int i = left; i < right; i++)
            {
                if (rc >= n2 || (lc < n1 && leftArray[lc].CompareTo(rightArray[rc]) <= 0))
                {
                    array[i] = leftArray[lc];
                    lc++;
                }
                else
                {
                    array[i] = rightArray[rc];
                    rc++;
                }
            }
        }

        public static void QuickSort<T>(List<T> array, int first, int last)
        where T : IComparable<T>
        {
            if (first >= last || last < 0)
                return;

            // Partition??§????????????(??????)???
            int pIndex = Partition(array, first, last);

            // ????????¨?????¨????????¨??????????????????????????¨???
            // (??????????????§array????????´??????????????§?????±??????????????????????????????????????????)
            QuickSort(array, first, pIndex - 1);
            QuickSort(array, pIndex + 1, last);
        }

        /// <summary>
        /// partition??§??????????????????????????????
        /// ??????????????????????´?????????????????????\??????
        /// ??????????????????????´???????????????????????????§?????????????????????array??????????????????
        /// (array???????????´?£???????????????????)???
        /// </summary>
        /// <param name="array"></param>
        /// <param name="first">??°??????????????¨??????????´??????????</param>
        /// <param name="last">??°??????????????¨??????????´???????(??????)???</param>
        /// <param name="partition">partition??¨??????????????????????´??????????</param>
        /// <returns>partition??????????±??????????????´??????????</returns>
        private static int Partition<T>(List<T> array, int first, int last, int partition = -1)
        where T : IComparable<T>
        {
            if (first < 0 || last >= array.Count)
                throw new System.ArgumentException("");

            if (partition == -1)
                // ?????????????????§???????°????????´?????????????????????£?????§???????????????
                partition = last;

            // ???????????£?????§???????±???¨?????????(last?????????????§????????????????)???
            T x = array[partition];
            Swap(array, partition, last);

            int i = first;
            for (int j = first; j < last; j++)
            {
                if (array[j].CompareTo(x) <= 0)
                {
                    Swap(array, i, j);
                    // ?????°i???Swap???????????´??°???????????????????????????????????¨????¢?????????????????????????????????????
                    i++;
                }
            }

            // ????°?????????????????????????x?????????????????£?????§??????????±??????????Swap???
            Swap(array, i, last);

            return i;
        }

        private static void Swap<T>(List<T> array, int i, int j)
        {
            T tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
        }

        public static void Solve()
        {
            int n = int.Parse(Console.ReadLine());

            var m_cards = new List<Card>();
            var q_cards = new List<Card>();
            for (int i = 0; i < n; i++)
            {
                string[] suit_and_num = Console.ReadLine().Split(' ');
                var card = new Card(suit_and_num[0], int.Parse(suit_and_num[1]));
                
                // MEMO:
                // ????????????????????????????????????????????´?£?????????????????????????????????§??????????????????
                // ?????????list????????????????´?????????§????????£??????????????¨?????¨??????
                m_cards.Add(card);
                q_cards.Add(card);
            }

            // ?????????????????§??????????????????
            MergeSort(m_cards, 0, n);
            QuickSort(q_cards, 0, n - 1);

            // ????????????????????¨????????????????????????
            // ?????????????????????????????????????????¨?¨??????????
            bool stable = true;
            for (int i = 0; i < n; i++)
            {
                stable = stable 
                    && m_cards[i].Suit == q_cards[i].Suit
                    && m_cards[i].Number == q_cards[i].Number;
            }
            Console.WriteLine("{0}", stable ? "Stable" : "Not stable");

            // Console.WriteLine("Merge Sorted Cards:");
            // m_cards.ForEach(card => Console.WriteLine(card));

            // Console.WriteLine("Quick Sorted Cards:");
            q_cards.ForEach(card => Console.WriteLine(card));
        }
        public static void Main()
        {
            // Console.SetIn(new System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/06_Sort_C_03.txt"));
            Solve();
        }
    }
}