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.io.IOException;
import java.util.ArrayDeque;

public class Main{
  public static void main(String[] args)throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] t = br.readLine().split(" ");
    int n = Integer.parseInt(t[0]);
    int q = Integer.parseInt(t[1]);
    ArrayDeque<P> Q = new ArrayDeque<P>();
    for(int i = 0; i < n; i++){
      t = br.readLine().split(" ");
      P p = new P();
      p.name = t[0];
      p.time = Integer.parseInt(t[1]);
      Q.addLast(p);
    }
    int sumTime = 0;
    StringBuilder sb = new StringBuilder();
    while(!Q.isEmpty()){
      P p = new P();
      p = Q.pollFirst();
      if(p.time - q <= 0){
        sumTime += p.time;
        sb.append(p.name).append(" ").append(sumTime).append("\n");
      }else{
        p.time = p.time - q;
        Q.addLast(p);
        sumTime = sumTime + q;
      }
    }
    System.out.print(sb);
  }
}

class P{
  String name;
  int time;
}