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

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();  // プロセス数
		int Q = sc.nextInt();  // クオンタム
		Map<String, Integer> hmap = new HashMap<String, Integer>();
		Queue<String> queue = new ArrayDeque<String>();  // 主役
		List<String> terminated = new ArrayList<String>();  // プロセスの名前を終わった順に格納する
		int timestamp = 0;

		for(int i = 0; i < N; i++) {
			String name = sc.next();
			int time = sc.nextInt();
			hmap.put(name, time);
			queue.add(name);
		}

		while(!queue.isEmpty()) {
			String p = queue.peek();
			if(hmap.get(p) <= Q) {  // クオンタム以内にプロセスpが終了する場合
				timestamp += hmap.get(p);
				terminated.add(queue.poll());
				hmap.put(p, timestamp);   // プロセスpが終了した時刻を記録
			}else {                 // クオンタム以内にプロセスpが終わらない場合
				timestamp += Q;
				queue.add(queue.poll());  // プロセスを待ち行列の最後尾へ
				hmap.put(p, hmap.get(p) - Q);
			}
		}

		for(String p : terminated) {
			System.out.println(p + " " + hmap.get(p));
		}

		sc.close();
	}
}