let rev_iter f a =
  let rec doit i =
    if i < 0 then ()
    else (f (Array.get a i); doit (i - 1))
  in
  doit (Array.length a - 1)

let counting_sort a b k get_val =
  let c = Array.make (k + 1) 0 in
  Array.iter (fun e -> let x = get_val e in c.(x) <- c.(x) + 1) a;
  Array.iteri (fun i _ -> if i = 0 then () else c.(i) <- c.(i) + c.(i - 1)) c;
  rev_iter (fun e -> let x = get_val e in b.(c.(x) - 1) <- e; c.(x) <- c.(x) - 1) a

let quick_sort a cmp =
  let partition p r =
    let x = a.(r) in
    let swap i j =
      let t = a.(i) in a.(i) <- a.(j); a.(j) <- t
    in
    let rec doit i j =
      if j >= r then i
      else begin
        if cmp a.(j) x <= 0 then (swap (i + 1) j; doit (i + 1) (j + 1))
        else doit i (j + 1)
      end
    in
    let i = doit (p - 1) p in
    swap (i + 1) r;
    i + 1
  in
  let rec doit p r =
    if p < r then begin
      let q = partition p r in
      doit p (q - 1);
      doit q r
    end
  in
  doit 0 (Array.length a - 1)

let stable_p a b equal =
  let n = Array.length a in
  let rec doit i =
    if i = n then true
    else if equal a.(i) b.(i) then doit (i + 1)
    else false
  in
  doit 0

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> Scanf.scanf "%c %d\n" (fun c d -> (c, d))) in
  let b = Array.copy a in
  let k = Array.fold_left (fun acc (_, d) -> max acc d) (-1) a in
  counting_sort a b k snd;
  quick_sort a (fun (_, x) (_, y) -> compare x y);
  Printf.printf "%s\n" (if stable_p a b (fun (xc, xd) (yc, yd) -> xc = yc && xd = yd) then "Stable" else "Not stable");
  Array.iter (fun (c, d) -> Printf.printf "%c %d\n" c d) a