
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE -1
#define MAX 100005
??
typedef struct pp {
????char name[10];
????int time;
} P;
??
P Q[MAX];
int head, tail, n;
??
void enqueue(P x);
P dequeue();
int isEmpty();
int isFull();
int min(int a, int b);
??
int main(){
????int elaps = 0, c;
????int i, q ;
????P u;
??
????scanf("%d %d", &n, &q);
??
????for( i=1 ; i<=n ; i++ ){
????????scanf("%s", Q[i].name);
????????scanf("%d", &Q[i].time);
????}
????head = 1;
????tail = n+1;
??
????while( head != tail){
????????u = dequeue();
????????c = min(q, u.time);
????????u.time -= c;
????????elaps += c;
????????if( u.time > 0 ){
????????????enqueue(u);
????????} else {
????????????printf("%s %d\n", u.name, elaps);
????????}
????}
return 0;
}
??
int isEmpty(){
????if(head == tail){
????????return TRUE;
????} else {
????????return FALSE;
????}
}
??
int isFull(){
????if(head == (tail + 1) % MAX){
????????return TRUE;
????} else {
????????return FALSE;
????}
}
??
void enqueue(P x){
??????if(isFull() == TRUE){
??????????fprintf(stderr, "overflow\n");
??????}
????????Q[tail] = x;
????????tail = (tail + 1) % MAX;
??}
??
P dequeue(){
????P x;
????if(isEmpty() == TRUE){
????????fprintf(stderr, "underflow\n");
????}
????x = Q[head];
????head = (head + 1) % MAX;
????return x;
}
??
int min(a,b){
????return a<b ? a:b ;
}