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

public class Main {
	
	private static class Process{
		public String n;
		public int t;
		
		public Process(String name, int time) {
			n = name;
			t = time;
		}
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String first = in.nextLine();
		String[] components = first.split(" ");
		int inputs = Integer.parseInt(components[0]);
		int quantum = Integer.parseInt(components[1]);
		int time = 0;
		
		//Creating the Queue of processes
		LinkedList<Process> q = new LinkedList<>();
		while (in.hasNextLine()) {
			String i = in.nextLine();
			String[] c = i.split(" ");
			q.add(new Process(c[0], Integer.parseInt(c[1])));
		}
		
		//Running
		while (!q.isEmpty()) {
			Process curr = q.poll();
			if (curr.t <= quantum) {
				time += curr.t;
				System.out.println(curr.n + " " + time);
			} else {
				curr.t -= quantum;
				time += quantum;
				q.add(curr);
			}
		}
	}
}