#include <stdio.h>
#include <stdlib.h>
#include<string.h>

struct node{
	char name[11];
	int time;
	struct node *next;
	struct node *prev;
};

typedef struct node *NodePointer;

NodePointer nil;

void init(){
	nil = malloc(sizeof(struct node));
	nil->next = nil;
	nil->prev = nil;
}

void deleteNode(NodePointer t){
	t->prev->next = t->next;
	t->next->prev = t->prev;
	free(t);
}

void backInsert(char name[],int time){
	NodePointer x;
	x = malloc(sizeof(struct node));
	strcpy(x->name,name);
	x->time = time;
	x->next = nil;
	x->prev = nil->prev;
	x->prev->next = x;
	nil->prev = x;
}

void deleteFirst(){
	NodePointer t = nil->next;
	if(t == nil){
		return;
	}
	deleteNode(t);
}

int main(int argc, char **argv)
{
	char name[11];
	int time = 0;
	int q = 0;
	int n = 0;
	int i = 0;
	int count = 0;

	scanf("%d%d",&n,&q);

	init();
	for(i = 0;i < n;i++){
		scanf("%s", name);
		scanf("%d", &time);
		backInsert(name,time);
	}
	
	NodePointer t = nil->next;

	i = 0;
	while(t != nil){
		i++;
		count++;
		t->time--;
		if(t->time == 0){
			printf("%s %d\n", t->name, count);
			deleteFirst();
			i = 0;
		}
		else if(i == q){
			backInsert(t->name,t->time);
			deleteFirst();
			i = 0;
		}
		t = nil->next;
	}

	free(nil);
	return 0;
}
