open Scanf
open Printf

type mlist = MNil | MCons of int * string * mlist ref
type queue = {mutable head : mlist; mutable tail : mlist}

let enque a s qu = 
 match qu with
 {head = MNil; tail = MNil} as q ->
 let c = MCons (a, s, ref MNil) in
 q.head <- c; q.tail <- c
 | {tail = MCons (_, _, next)} as q ->
 let c = MCons (a, s, ref MNil) in
 next := c; q.tail <- c
 | _ -> ()

let deque = function
 {head = MNil; tail = MNil} ->
   (-1, "error")
 |{head = MCons (a, s, q')} as q -> 
   q.head <- !q'; (a, s)
 | _ -> 
  (-1, "error")

let n, qt = scanf "%d %d\n" (fun x y -> (x, y))

let rec input i q =
 match i with
 0 -> q
|i -> 
  let s, a = scanf "%s %d\n" (fun x y -> (x, y)) in
  enque a s q; input (i-1) q 

let () =
 let q = input n {head = MNil; tail = MNil} in
 let rec rrs que t = 
  let a, s = deque que in
  let t' = t + a in
  if (a = -1) && (s = "error") then ()
  else
   if a < qt then
    (printf "%s %d\n" s t'; rrs que t')
   else
    (enque a s que; rrs que t')
 in
 rrs q 0