object Main extends App {


  val (n, q)                        = readLine.split(" ").map(_.toInt) match { case Array(n, q) => (n, q)}
  val init: IndexedSeq[(String, Int)] = (1 to n)
    .map(_ => readLine)
    .map(_.split(" ") match {
      case Array(name, time) => (name, time.toInt)
    })
    .toIndexedSeq

  def exec(s: (IndexedSeq[(String, Int)], IndexedSeq[(String, Int)], Int)): (IndexedSeq[(String, Int)], IndexedSeq[(String, Int)], Int) = s match {
    case (h +: tail, acc, total) => if (h._2 <= q) (tail, (h._1, h._2 + total) +: acc, total + h._2) else (tail :+ (h._1, h._2 - q), acc, total + q)
    case _ => ???
  }

  val last = Stream.iterate((init, IndexedSeq[(String, Int)](), 0))(exec).span(_._1.nonEmpty) match {
    case (h, t) => t.head
  }
  last._2.sortBy(_._2).foreach {
    case (name, time) => println(name + " " + time)
  }

}
