require 'thread'

class MyProcess
  def initialize(name, quantum)
    @name = name
    @quantum = quantum
  end
  attr_accessor :name, :quantum

  def to_str
    "#{name} #{quantum}"
  end
end

def round_robin_scheduling(queue, quantum)
  time = 0
  array = []
  until queue.empty?
    process = queue.deq
    if process.quantum - quantum <= 0
      time += quantum + process.quantum - quantum
      array << { name: process.name, time: time }
    else
      process.quantum = process.quantum - quantum
      time += quantum
      queue.enq process
    end
  end
  array
end

n, q = gets.split(' ').map(&:to_i)
queue = Queue.new
n.times do
  input = gets.split(' ')
  queue.enq MyProcess.new(input[0], input[1].to_i)
end

result = round_robin_scheduling queue, q
result.each do |hash|
  puts "#{hash.fetch :name} #{hash.fetch :time}"
end

