import Control.Monad
import Data.List (foldl', unfoldr)

type Queue a = ([a], Int, [a], Int)

empty' :: Queue a
empty' = ([], 0, [], 0)

isEmpty' :: Queue a -> Bool
isEmpty' (_, 0, _, _) = True
isEmpty' _ = False

head' :: Queue a -> a
head' (x : _, _, _, _) = x
head' _ = error "the queue is empty"

check :: Queue a -> Queue a
check q@(f, fl, t, tl)
  | fl < tl = (f ++ reverse t, fl + tl, [], 0)
  | otherwise = q

tail' :: Queue a -> Queue a
tail' (_ : f, fl, t, tl) = check (f, fl - 1, t, tl)
tail' _ = error "the queue is empty"

snoc' :: Queue a -> a -> Queue a
snoc' (f, fl, t, tl) x = check (f, fl, x : t, tl + 1)

main = do
  [n, q] <- fmap (map read . words) getLine
  let readPair :: IO (String, Int)
      readPair = do
        [a, b] <- fmap words getLine
        return $ (a, read b)
  qu <- fmap (foldl' snoc' empty') $ replicateM n readPair
  let f qu ti o
        | isEmpty' qu = o
        | l <= q = let ti' = ti + l
                   in f t ti' ((n, ti') : o)
        | otherwise = let qu' = snoc' t (n, l - q)
                          ti' = ti + q
                      in f qu' ti' o
        where
          (n, l) = head' qu
          t = tail' qu
      o = reverse $ f qu 0 []
  mapM_ (\(n, t) -> putStrLn $ n ++ " " ++ show t) o