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.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner scan = new Scanner(System.in);
		int num, quantum = 0;
		int time = 0; // time;総時間数
		Queue<String> qName = new ArrayDeque<String>();
		Queue<Integer> qTime = new ArrayDeque<Integer>();

		num = scan.nextInt(); // 入力数
		quantum = scan.nextInt(); // クオンタム（実行する処理時間）
		scan.nextLine();

		for (int i = 0; i < num; i++) {
			qName.add(scan.next());
			qTime.add(scan.nextInt());
		}

		while (!qName.isEmpty()) {
			String qNamePolled = qName.poll();
			int getTime = qTime.poll();

			if (getTime > quantum) { // 総時間数をカウントしていく
				time = time + quantum;
			} else {
				time = time + getTime;
			}

			if (getTime > quantum) {
				qTime.add(getTime - quantum);
				qName.add(qNamePolled);
			} else { // 今のプロセス処理終了
				System.out.println(qNamePolled + " " + time);
			}
		}
		scan.close();
	}
}