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();
        }
    }
}

Measured memory_bytes: 24280