import Control.Applicative
import Control.Monad
import Control.Monad.State

main = do
        [n, q] <- map read.words<$>getLine
        pstrs  <- replicateM n getLine
        let procs = setProcess pstrs
        simulateProcess q procs

simulateProcess :: Int -> Queue Process -> IO ()
simulateProcess q = evalStateT (evalStateT (roundRobin q) 0)

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

enQueue :: Monad m => a -> StateT (Queue a) m ()
enQueue x = get >>= \(as,xs) -> put ((as, x:xs))

deQueue :: Monad m => StateT (Queue a) m a
deQueue = do
        (as, xs) <- get
        if null as then do
                       let xs' = reverse xs
                       put (tail xs', [])
                       return $ head xs'
                   else do
                       put (tail as, xs)
                       return $ head as

isEmpty :: Monad m => StateT (Queue a) m Bool
isEmpty = do
        (as,xs) <- get
        return $ null as && null xs


data Process = P { name :: String
                 , time :: Int } deriving (Show)

process :: [String] -> Process
process [x,y] = P x (read y)

setProcess :: [String] -> Queue Process
setProcess pstrings = execState action ([],[])
        where ps = map (process.words) $ pstrings
              action = sequence_ $ map enQueue ps

roundRobin :: Int -> StateT Int (StateT (Queue Process) IO) ()
roundRobin q = do
        t <- get
        p <- lift deQueue
        runProcess q p t
        emptyCheck
        where emptyCheck = do
                empty <- lift isEmpty
                if empty then return ()
                         else roundRobin q

runProcess :: Int -> Process -> Int -> StateT Int (StateT (Queue Process) IO) ()
runProcess q p t
    | q >= time p = do
        liftIO (putStrLn $ unwords [name p, show (t+time p)])
        modify (+ time p)
    | otherwise   = do
        let p' = p {time = time p - q}
        modify (+ q)
        lift $ enQueue p'