output_description: 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.
input_description: 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.
attempt: from sys import stdin
  
  
  def partition(cards, start, end):
      p = cards[end]
      p_idx = start
      for i in range(start, end):
          c = cards[i]
          if c[1] <= p[1]:
              cards[i], cards[p_idx] = cards[p_idx], cards[i]
              p_idx = p_idx + 1
      cards[p_idx], cards[end] = cards[end], cards[p_idx]
      return p_idx
  
  
  # in-place quicksort
  def quicksort(cards, start, end):
      if start < end:
          p_idx = partition(cards, start, end)
          quicksort(cards, start, p_idx - 1)
          quicksort(cards, p_idx + 1, end)
  
  
  def merge(nums, left, mid, right):
      l = nums[left:mid] + [("_", 9999999999)]
      r = nums[mid:right] + [("_", 9999999999)]
      l_cur = 0
      r_cur = 0
      for i in range(right - left):
          if l[l_cur][1] > r[r_cur][1]:
              nums[left + i] = r[r_cur]
              r_cur = r_cur + 1
          else:
              nums[left + i] = l[l_cur]
              l_cur = l_cur + 1
  
  
  def mergesort(nums, left, right):
      if right - left <= 1:
          return nums
      else:
          mid = (left + right) // 2
          mergesort(nums, left, mid)
          mergesort(nums, mid, right)
          merge(nums, left, mid, right)
  
  
  def is_stable_result(orig, result):
      stable_result = orig[:]
      mergesort(stable_result, 0, len(stable_result))
      is_stable = True
      for c1, c2 in zip(stable_result, result):
          if c1 != c2:
              is_stable = False
      return is_stable
  
  
  def parseLine(l):
      kind, num = l.strip().split(" ")
      return (kind, int(num))
  
  
  def main():
      _, *lines = stdin.readlines()
      nums = [parseLine(l) for l in lines]
  
      # sort
      result = nums[:]
      quicksort(result, 0, len(result) - 1)
  
      # check is stable
      is_stable = is_stable_result(nums, result)
      print("Stable" if is_stable else "Not stable")
      print("\n".join([kind + " " + str(num) for (kind, num) in result]))
  
  
  if __name__ == "__main__":
      main()
  
problem_description: 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).
samples: [{"input": "6\n    D 3\n    H 2\n    D 1\n    S 3\n    D 2\n    C 1", "output": "Not stable\n    D 1\n    C 1\n    D 2\n    H 2\n    D 3\n    S 3"}, {"input": "2\n    S 1\n    H 1", "output": "Stable\n    S 1\n    H 1"}]