object Main extends App {

  case class State[S, A](run: S => (S, A)) {
    def map[B](f: A => B) =
      State((s: S) => {
        val (s1, a) = run(s)
        (s1, f(a))
      })

    def flatMap[B](f: A => State[S, B]) =
      State((s: S) => {
        val (s1, a) = run(s)
        f(a).run(s1)
      })
  }

  object State {
    def unit[S, A](a: A): State[S, A] = State(s => (s, a))
  }

  implicit class ListOps[S, A](stlist: List[State[S, A]]) {
    def sequence: State[S, List[A]] =
      stlist.foldRight(State.unit[S, List[A]](List[A]()))(
        (h: State[S, A], acc: State[S, List[A]]) =>
          h.flatMap(a => acc.map(list => a :: list)))
  }

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


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

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

}

