class Proces
  def initialize(name,time)
    @name = name
    @time = time.to_i
  end
  def act limit
    if @time > limit
      @time -= limit
      true
    end
    false
  end
  attr_reader :name,:time
end

queue = Array.new()
time = 0
temp = gets.split(" ").map{|x| x.to_i}
q = temp.pop
num =  temp.pop
num.times do
 temp = gets.split(" ")
 queue.push(Proces.new(temp[0],temp[1]))
end
until (p = queue.shift) == nil ? true:false
 if p.act(q)
  queue.push(p)
  time += q
 else
  time+=p.time
  printf("%s %d\n",p.name,time) 
 end
end