{-# LANGUAGE BangPatterns #-}

import Control.Applicative ((<$>))
import Data.Maybe (fromJust)
import Data.Vector.Unboxed (Vector, unsafeFreeze, unsafeThaw, fromList, toList)
import Data.Vector.Unboxed.Mutable (IOVector, unsafeRead, unsafeSwap)
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as MV
import qualified Data.ByteString.Char8 as BS

type Start  = Int
type End    = Int
type Order  = Int
type Appear = Int
type Card   = (Appear, Char, Order)

ft :: Card -> Int
ft (a, _, _) = a
td :: Card -> Int
td (_, _, a) = a

toCard :: [(Int, [String])] -> [Card]
toCard [] = []
toCard ((idx, [a, x]):rest) = (idx, head a, read x) : toCard rest

main :: IO ()
main
    =   getLine
    >>  fromList . toCard . zip [0..] . map words . lines <$> getContents
    >>= unsafeThaw
    >>= \cards -> quickSort 0 (MV.length cards - 1) cards
    >>= isStable 0
    >>  unsafeFreeze cards
    >>= mapM_ toPrint . toList


quickSort :: Start -> End -> IOVector Card -> IO (IOVector Card)
quickSort start end cards
    | start >= end = return cards
    | otherwise    = do
        p <- partition start end cards
        quickSort start (p - 1) cards
        quickSort (p + 1) end cards


partition :: Start -> End -> IOVector Card -> IO Int
partition !start !end !cards = do
    card <- unsafeRead cards end
    checkAndSwap cards (td card) (start - 1) start
  where
    checkAndSwap !cards !pivot !i !j
        | j == end  = do
            unsafeSwap cards (i + 1) end
            return (i + 1)
        | otherwise = do
            card <- unsafeRead cards j
            case (td card) <= pivot of
                True -> do
                    unsafeSwap cards (i + 1) j
                    checkAndSwap cards pivot (i + 1) (j + 1)

                False -> do
                    checkAndSwap cards pivot i (j + 1)


isStable :: Int -> IOVector Card -> IO ()
isStable i cards
    | MV.length cards - 1 == i =
        putStrLn "Stable"

    | otherwise = do
        card1 <- unsafeRead cards i
        card2 <- unsafeRead cards (i + 1)
        if (td card1) == (td card2) && (ft card1) > (ft card2) then
            putStrLn "Not stable"
        else
            isStable (i + 1) cards


toPrint :: Card -> IO ()
toPrint (_, c, x) = putStrLn $ c : ' ' : show x
