Buggy python submission (verdict: Time Limit Exceeded):
nq=list(map(int,input().split()))
n=[]
p=[]
t=[]
ans1=[]
ans2=[]
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	p.append(0)
	t.append(inp[0])

cnt=0
time=0
while n!=p:
	if n[0]==0:
		x=0
		y=0
		x=n[0]
		y=t[0]
		for j in range(len(n)-1):
			n[j]=n[j+1]
			t[j]=t[j+1]
		n[len(n)-1]=x
		t[len(t)-1]=y
	
	else:	
		n[0]=n[0]-nq[1]
		if n[0]<=0:
			time=time+(int(nq[1])*(cnt+1)+int(n[0]))
			cnt=0
			n[0]=0
			x=0
			y=0
			x=n[0]
			y=t[0]
			for j in range(len(n)-1):
				n[j]=n[j+1]
				t[j]=t[j+1]
			n[len(n)-1]=x
			t[len(t)-1]=y
			ans1.append(t[len(t)-1])
			ans2.append(time)
			
			
		else:
			x=0
			y=0
			x=n[0]
			y=t[0]
			for j in range(len(n)-1):
				n[j]=n[j+1]
				t[j]=t[j+1]
			n[len(n)-1]=x
			t[len(t)-1]=y
			cnt+=1
for i in range(nq[0]):
	print(ans1[i],ans2[i])


Fixed submission:
n=[]
t=[]
nq=list(map(int,input().split()))
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	t.append(inp[0])
cnt1=0
while len(n)!=0:	
	n[0]=n[0]-nq[1]
	if n[0]<=0:
		cnt1=cnt1+nq[1]+n[0]
		print(t[0],cnt1)
		n.pop(0)
		t.pop(0)	
		
	else:
		n.append(n[0])
		t.append(t[0])
		n.pop(0)
		t.pop(0)
		cnt1+=nq[1]

Buggy python submission (verdict: Time Limit Exceeded):
nq=list(map(int,input().split()))
n=[]
p=[]
t=[]
ans1=[]
ans2=[]
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	p.append(0)
	t.append(inp[0])

cnt=0
time=0
while n!=p:
	if n[0]==0:
		x=0
		y=0
		x=n[0]
		y=t[0]
		for j in range(len(n)-1):
			n[j]=n[j+1]
			t[j]=t[j+1]
		n[len(n)-1]=x
		t[len(t)-1]=y
	
	else:	
		n[0]=n[0]-nq[1]
		if n[0]<=0:
			time=time+(int(nq[1])*(cnt+1)+int(n[0]))
			cnt=0
			n[0]=0
			x=0
			y=0
			x=n[0]
			y=t[0]
			for j in range(len(n)-1):
				n[j]=n[j+1]
				t[j]=t[j+1]
			n[len(n)-1]=x
			t[len(t)-1]=y
			ans1.append(t[len(t)-1])
			ans2.append(time)
			
			
		else:
			x=0
			y=0
			x=n[0]
			y=t[0]
			for j in range(len(n)-1):
				n[j]=n[j+1]
				t[j]=t[j+1]
			n[len(n)-1]=x
			t[len(t)-1]=y
			cnt+=1
for i in range(nq[0]):
	print(ans1[i],ans2[i])
	
	
	
	


	


Fixed submission:
n=[]
t=[]
nq=list(map(int,input().split()))
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	t.append(inp[0])
cnt1=0
while len(n)!=0:	
	n[0]=n[0]-nq[1]
	if n[0]<=0:
		cnt1=cnt1+nq[1]+n[0]
		print(t[0],cnt1)
		n.pop(0)
		t.pop(0)	
		
	else:
		n.append(n[0])
		t.append(t[0])
		n.pop(0)
		t.pop(0)
		cnt1+=nq[1]

Buggy python submission (verdict: Time Limit Exceeded):
n=[]
t=[]
nq=list(map(int,input().split()))
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	t.append(inp[0])
cnt1=0
while len(n)!=0:	
	n[0]=n[0]-nq[1]
	if n[0]<=0:
		cnt1=cnt1+nq[1]+n[0]
		print(t[0],cnt1)
		n.pop(0)
		t.pop(0)		
	else:
		x=0
		y=0
		x=n[0]
		y=t[0]
		for j in range(len(n)-1):
			n[j]=n[j+1]
			t[j]=t[j+1]
		n[len(n)-1]=x
		t[len(t)-1]=y
		cnt1+=nq[1]
	
		


Fixed submission:
n=[]
t=[]
nq=list(map(int,input().split()))
for i in range(nq[0]):
	inp=input().split()
	n.append(int(inp[1]))
	t.append(inp[0])
cnt1=0
while len(n)!=0:	
	n[0]=n[0]-nq[1]
	if n[0]<=0:
		cnt1=cnt1+nq[1]+n[0]
		print(t[0],cnt1)
		n.pop(0)
		t.pop(0)	
		
	else:
		n.append(n[0])
		t.append(t[0])
		n.pop(0)
		t.pop(0)
		cnt1+=nq[1]