solve :: Int -> [[String]] -> Int -> [String] -> [String]
solve _ [] _ list = list
solve q (all@[a,b]:xs) time list
  | ((read b) - q) <= 0 = solve q xs (time + read b) ((a ++ " " ++ (show . (time +) . read) b):list)
  | otherwise = solve q (xs ++ [a,(show . (\x -> x - q) . read) b]:[]) (time + q) list

main = do
  ([_,qs]:xs) <- fmap (map words . lines) getContents
  mapM_ putStrLn $ reverse $ solve (read qs) xs 0 []
