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.Scanner;

class Data{
	String name;
	int time;
	Data next;
	
	Data(String s,int t){
		name=s;
		time=t;
		next=null;
	}
}

public class Main {
	static Data first=new Data("",0);
	static Data attention=first;
	
	static Data dequeue(){
		Data tmp=first.next;
		if(attention==tmp){
			attention=first;
		}
		first.next=tmp.next;
		return tmp;
	}
	
	static void enqueue(String s,int t){
		attention.next=new Data(s,t);
		attention=attention.next;
	}

	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();//プロセス数。
		int q=scan.nextInt();//クォンタム
		int finish=0;
		String s;
		int t;
		for(int i=0;i<n;i++){
			s=scan.next();
			t=scan.nextInt();
			enqueue(s,t);
		}

		while(first.next!=null){
			Data tmp=dequeue();
			s=tmp.name;
			t=tmp.time;
			finish+=q;
			if(t-q<=0){//プロセスが終了する場合
				finish=finish+(t-q);
				System.out.printf("%s %d\n",s,finish);
			}else{//プロセスがまだ終了しない場合
				enqueue(s,t-q);
			}
		}
		
		
		scan.close();
		
	}

}