Problem: Notes

Template in C

Constraints

* 1 ≤ n ≤ 100000
* 1 ≤ q ≤ 1000
* 1 ≤ timei ≤ 50000
* 1 ≤ length of namei ≤ 10
* 1 ≤ Sum of timei ≤ 1000000

Input

n q
name1 time1
name2 time2
...
namen timen


In the first line the number of processes n and the quantum q are given separated by a single space.

In the following n lines, names and times for the n processes are given. namei and timei are separated by a single space.

Output

For each process, prints its name and the time the process finished in order.

Example

Input

5 100
p1 150
p2 80
p3 200
p4 350
p5 20


Output

p2 180
p5 400
p1 450
p3 550
p4 800

Solution (in JAVA):

Solution:
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String[] str = s.split(" ");
		long timer = 0;
		int time = 0;
		int n = Integer.parseInt(str[0]);
		int q = Integer.parseInt(str[1]);
		String name = new String();
		Queue<Integer> queue = new LinkedList<Integer>();
		Queue<String> namequeue = new LinkedList<String>();
		for(int i=0;i<n;i++){
			s = br.readLine();
			str = s.split(" ");
			namequeue.add(str[0]);
			queue.add(Integer.parseInt(str[1]));
		}
		while(queue.isEmpty()!=true){
			time=queue.poll();
			name=namequeue.poll();
			if(time>q){
				queue.add(time-q);
				namequeue.add(name);
				timer+=q;
			}
			else{
				timer+=time;
				System.out.println(name+" "+timer);
			}
		}
	}
}