import scala.io.StdIn.{readInt, readLine}
object Main {
    def main(args: Array[String]): Unit = {
        val len = readInt
        val arr = new Array[Card](len).map { a => 
            val line = readLine.split(" ")
            Card(line(0), line(1).toInt)
        }
        val arr2 = new Array[Card](len)
        arr.copyToArray(arr2)
        quickSort(arr)
        mergeSort(arr2)
        val q = arr.mkString("\n")
        val m = arr2.mkString("\n")
        if (q == m) println("Stable") else println("Not stable")
        println(q)
    }
    def quickSort(arr: Array[Card]): Unit = {
        def go(left: Int, right: Int): Unit = {
            if (left < right) {
                val (pivot, oneKind) = partition(arr, left, right)
                if (!oneKind) {
                    go(left, pivot - 1)
                    go(pivot + 1, right)
                }
            }
        }
        go(0, arr.length - 1)
    }
    def partition(arr: Array[Card], left: Int, right: Int): (Int, Boolean) = {
        val pivot = arr(right)
        def go(sep: Int, seek: Int, oneKind: Boolean): (Int, Boolean) = seek match {
            case s if s <= right => arr(seek) match {
                case e if e > pivot => go(sep, seek + 1, false)
                case e if e < pivot => {
                    swap(arr, sep + 1, seek)
                    go(sep + 1, seek + 1, false)
                }
                case e => {
                    swap(arr, sep + 1, seek)
                    go(sep + 1, seek + 1, oneKind)
                }
            }
            case s => (sep, oneKind)
        }
        go(left - 1, left, true)
    }
    def mergeSort(arr: Array[Card]): Unit = {
        def go(left: Int, right: Int): Unit = {
            if (left < right) {
                val mid = (left + right) / 2
                go(left, mid)
                go(mid + 1, right)
                merge(left, mid, right)
            }
        }
        def merge(left: Int, mid: Int, right: Int): Unit = {
            val tmp = arr.slice(left, mid + 1)
            var leftPtr = 0
            var rightPtr = mid + 1
            for (i <- (left to right)) {
                if (leftPtr >= tmp.length) {
                    rightPtr += 1
                } else if (rightPtr > right) {
                    arr(i) = tmp(leftPtr)
                    leftPtr += 1
                } else {
                    if (tmp(leftPtr) <= arr(rightPtr)) {
                        arr(i) = tmp(leftPtr)
                        leftPtr += 1
                    } else {
                        arr(i) = arr(rightPtr)
                        rightPtr += 1
                    }
                }
            }
        }
        go(0, arr.length - 1)
    } 

    def swap(arr: Array[Card], a: Int, b: Int): Unit = {
        val tmp = arr(a)
        arr(a) = arr(b)
        arr(b) = tmp
    }
}
case class Card(symbol: String, num: Int) {
    override def toString(): String = symbol + " " + num
    def <(a: Card): Boolean = this.num < a.num
    def <=(a: Card): Boolean = this.num <= a.num
    def >(a: Card): Boolean =  this.num > a.num
    def >=(a: Card): Boolean =  this.num >= a.num
}
