open Scanf
open Printf

type que = NULL | QUE of string * int * que
let n, b = scanf "%d %d\n" (fun x y -> (x, y))

let rec input q i =
 if i = n then q
 else
  let name, time = scanf "%s %d\n" (fun x y -> (x, y)) in
  match q with
  NULL -> input (QUE (name, time, NULL)) (i+1)
  |q' -> input (QUE (name, time, q')) (i+1)

let rec insert_last q q_last =
 match q with 
 NULL -> NULL
 |QUE (na, ti, NULL) -> QUE (na, ti, q_last)
 |QUE (na, ti, q') -> QUE (na, ti, (insert_last q' q_last))

let () =
 let rec f q t =
  match q with
  NULL -> ()
 |QUE (name, time, q') -> 
  if time <= b then
   let t' = time + t in
   printf "%s %d\n" name t'; f q' t'
  else
   f (insert_last q' (QUE (name, time, NULL))) (time+b)
 in
 f (input NULL 0) 0