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:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;

class Main {

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		ArrayDeque<Process> queue = new ArrayDeque<Process>();
		String[] str01 = br.readLine().split(" ");
		int n = Integer.parseInt(str01[0]);
		int point = Integer.parseInt(str01[1]);
		int sumTime = 0;
		

		for (int i = 0; i < n; i++) {
			String[] str02 = br.readLine().split(" ");
			Process process = new Process(str02[0], Integer.parseInt(str02[1]));
			queue.add(process);
		}
		int i = 0;
		while (true) {
			Process temp = queue.poll();
			if (temp.time <= point) {
				sumTime = sumTime + temp.time;
				temp.time = sumTime;
				System.out.println(temp.name + " " + temp.time);
				i++;
				if(i == n){
					break;
				}
			} else {
				temp.time = temp.time - point;
				queue.add(temp);
				sumTime = sumTime + point;
			}
		}
	}
}

class Process {
	String name;
	int time;

	Process(String name, int time) {
		this.name = name;
		this.time = time;
	}
}