let swap a i j =
  let temp = a.(i) in
  a.(i) <- a.(j);a.(j) <- temp;;

let rec find a x n =
  if a.(n) = x then n else find a x (n+1);;

let is_stable org c n =
  let rec loop = function
      i when i = n -> "Stable"
    | i ->
       if (snd c.(i)) = (snd c.(i-1)) then
         if find org c.(i) 0 > find org c.(i-1) 0 then loop (i+1)
         else "Not stable"
       else
         loop (i+1)
  in loop 1
;;

let partition a p r =
  let x = a.(r) and
      i = ref (p-1) in
  for j = p to (r-1) do
    if (snd a.(j)) <= (snd x) then
      begin
        i := (!i) + 1;
        swap a (!i) j;
      end;
  done;
  swap a ((!i)+1) r;
  (!i) + 1
;;

let rec quick_sort a p r =
  if p < r then
    let q = partition a p r in
    quick_sort a p (q-1);
    quick_sort a (q+1) r
;;

let () =
  let n = read_int () in
  let a1 = Array.init n (fun x -> Scanf.scanf "%s %d\n" (fun y z-> (y,z))) in
  let a2 = Array.copy a1 in
  quick_sort a2 0 (n-1);
  print_endline (is_stable a1 a2 n);
  Array.iter (fun x -> Printf.printf "%s %d\n" (fst x) (snd x)) a2
;;