data Queue a = Queue [a] [a]
enqueue::a->Queue a->Queue a
enqueue x (Queue xs ys) = Queue (x:xs) ys
dequeue::Queue a->(a, Queue a)
dequeue (Queue xs []) = dequeue (Queue [] (reverse xs))
dequeue (Queue xs (y:ys)) = (y, Queue xs ys)
toInt::String->Int
toInt = read
sub::[String]->(String, Int)
sub (x:y:_) = (x, toInt y)
solve::Int->Int->Queue (String, Int)->String
solve _ _ (Queue [] []) = ""
solve t q queue
  | x2 > q = solve (t+q) q (enqueue (x1, x2-q) queue')
  | otherwise = x1 ++ " " ++ show (t+x2) ++ "\n" ++ solve (t+x2) q queue'
    where
      ((x1, x2), queue') = dequeue queue
main::IO ()
main = do
  a <- getLine
  let _:q:_ = words a
  getContents >>=
    putStr.(solve 0 (toInt q)).(foldl (flip enqueue) (Queue [] [])).(map (sub.words)).lines
