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

Source: aizu | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
import sys
from collections import deque
s = sys.stdin.readlines()
(_, q) = map(int, s[0].split())
f = lambda x, y: (x, int(y))
d = deque((f(*e.split()) for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


from collections import deque
(n, q) = map(int, input().split())
deq = deque([])
for i in range(n):
	A = input().split()
	s = A[0]
	m = int(A[1])
	deq.append([s, m])
ans = 0
while deq:
	p = deq.popleft()
	if 0 < p[1] <= q:
		ans += p[1]
		print(p[0], ans)
	else:
		ans += q
		p[1] -= q
		deq.append([p[0], p[1]])


from collections import deque
(n, q) = map(int, input().split())
que = deque([])
for _ in range(n):
	(p, t) = input().split()
	que.append((p, int(t)))
tm = 0
while len(que):
	(n1, n2) = que.popleft()
	if n2 > q:
		n2 -= q
		que.append((n1, n2))
		tm += q
	elif n2 == q:
		print(n1, tm + q)
		tm += q
	else:
		print(n1, tm + n2)
		tm += n2


(n, q) = map(int, input().split())
p = [input().split() for _ in range(n)]
time = 0
while len(p) > 0:
	left = int(p[0][1]) - q
	if left <= 0:
		time = time + int(p[0][1])
		print(p[0][0], end='')
		print(' ', end='')
		print(time)
		p.pop(0)
	else:
		time = time + q
		p[0][1] = left
		p.append(p[0])
		p.pop(0)


from collections import deque as queue
(N, Q) = map(int, input().split())
q = queue()
for i in range(N):
	(n, t) = input().split()
	q.append((n, int(t)))
total = 0
while len(q) != 0:
	(n, t) = q.popleft()
	if t <= Q:
		total += t
		print(n, total)
	else:
		total += Q
		q.append((n, t - Q))


(n, q) = map(int, input().split())
ntlist = []
for i in range(n):
	nt = list(map(str, input().split()))
	ntlist.append(nt)
tp = 0
while len(ntlist):
	nt = ntlist.pop(0)
	if int(nt[1]) > q:
		nt[1] = int(nt[1]) - q
		tp += q
		ntlist.append(nt)
	else:
		tp += int(nt[1])
		nt[1] = 0
		print(nt[0], tp)


from collections import deque
d = deque()
(n, q) = map(int, input().split())
for i in range(n):
	d.append(input().split())
now = 0
while d:
	(name, time) = d.popleft()
	time = int(time)
	if time <= q:
		now += time
		print(name, now)
	else:
		time -= q
		d.append([name, time])
		now += q


(n, q) = map(int, input().split())
tmp = [input().split() for i in range(n)]
a = [[name, int(time)] for (name, time) in tmp]
total_time = 0
count = 0
while a:
	current = a.pop(0)
	if current[1] > q:
		current[1] -= q
		a.append(current)
		total_time += q
	else:
		total_time += current[1]
		print(current[0], total_time, sep=' ')


from collections import deque
(n, q) = map(int, input().split())
p = deque()
for i in range(n):
	(name, t) = input().split()
	time = int(t)
	p.append((name, time))
elapsed = 0
while len(p) > 0:
	(name, time) = p.popleft()
	elapsed += min(q, time)
	if time > q:
		p.append((name, time - q))
	else:
		print(name, elapsed)


A = list(map(int, input().split()))
B = []
q = A[1]
from collections import deque
D = deque()
for i in range(A[0]):
	L = list(map(str, input().split()))
	L[1] = int(L[1])
	D.append(L[:])
time = 0
while len(D):
	a = D.popleft()
	if a[1] <= q:
		time += a[1]
		print(f'{a[0]} {time}')
	else:
		time += q
		D.append([a[0], a[1] - q])


(n, q) = list(map(int, input().split()))
que = []
for i in range(n):
	(p, t) = input().split()
	que.append([p, int(t)])
total = 0
while True:
	if q >= que[0][1]:
		total += que[0][1]
		print(que[0][0], total)
		que.pop(0)
	else:
		total += q
		que[0][1] -= q
		que.append(que[0])
		que.pop(0)
	if len(que) == 0:
		break


(n, m) = map(int, input().split())
p = [input().split() for _ in range(n)]
cnt = 0
while p:
	if int(p[0][1]) > m:
		p[0][1] = int(p[0][1]) - m
		x = p[0]
		del p[0]
		p.append(x)
		cnt += m
	else:
		cnt += int(p[0][1])
		print(p[0][0], cnt)
		del p[0]


from collections import deque
(n, q) = [int(_) for _ in input().split()]
queue = deque()
for _ in range(n):
	(name, ti) = input().split()
	queue.append((name, int(ti)))
t = 0
while queue:
	(name, ti) = queue.popleft()
	if ti <= q:
		print(name, t + ti)
		t += ti
	else:
		queue.append((name, ti - q))
		t += q


from collections import deque
(n, q) = map(int, input().split())
qu = deque([])
time = 0
for i in range(n):
	(a, b) = input().split()
	qu.append([a, int(b)])
while len(qu) > 0:
	c = qu.popleft()
	if c[1] <= q:
		time += c[1]
		print(c[0], time)
	else:
		time += q
		qu.append([c[0], c[1] - q])


(n, q) = map(int, input().split())
a = []
t = 0
for i in range(n):
	a.append(input().split())
while len(a) != 0:
	A = a.pop(0)
	if int(A[1]) <= q:
		t += int(A[1])
		print(A[0], t)
	else:
		t += q
		A[1] = int(A[1]) - q
		a.append(A)


(n, q) = map(int, input().split())
a = []
t = 0
for i in range(n):
	a.append(input().split())
while len(a) != 0:
	proc = a.pop(0)
	if int(proc[1]) <= q:
		t += int(proc[1])
		print(proc[0], t)
	else:
		t += q
		proc[1] = int(proc[1]) - q
		a.append(proc)


import sys
from collections import deque
s = sys.stdin.readlines()
q = int(s[0].split()[1])
f = lambda x, y: (x, int(y))
d = deque((f(*e.split()) for e in s[1:]))
(t, a) = (0, [])
while d:
	(k, v) = d.popleft()
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		a += [f'{k} {t}']
print('\n'.join(a))


from collections import deque
(n, q) = [int(_) for _ in input().split()]
processes = deque([tuple(input().split()) for i in range(n)])
time = 0
while processes:
	process = processes.popleft()
	if int(process[1]) <= q:
		time += int(process[1])
		print(process[0], time)
	else:
		time += q
		processes.append((process[0], int(process[1]) - q))


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	x = input().split()
	queue.append((x[0], int(x[1])))
time = 0
while queue != []:
	if queue[0][1] <= q:
		time += queue[0][1]
		print('{} {}'.format(queue.pop(0)[0], time))
	else:
		time += q
		x = queue.pop(0)
		queue.append((x[0], x[1] - q))


from collections import deque
(n, q) = map(int, input().split())
queue = deque()
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
t = 0
while queue:
	p = queue.popleft()
	t += min(q, p[1])
	if q < p[1]:
		queue.append([p[0], p[1] - q])
	else:
		print(f'{p[0]} {t}')


(n, q) = map(int, input().split())
my_list = [input().split() for s in range(n)]
count = 0
while my_list != []:
	a = my_list.pop(0)
	n1 = a[0]
	n2 = int(a[1])
	if n2 > q:
		n2 -= q
		count += q
		my_list.append([n1, n2])
	else:
		count += n2
		print(n1, count)


import queue
A = queue.Queue()
(n, q) = map(int, input().split())
for i in range(n):
	(name, time) = input().split()
	A.put([name, int(time)])
t = 0
while not A.empty():
	L = A.get()
	if L[1] > q:
		L[1] -= q
		A.put(L)
		t += q
	else:
		t += L[1]
		print(L[0], t)


(n, q) = map(int, input().split())
data = []
for i in range(n):
	data.append(list(input().split()))
	data[i][1] = int(data[i][1])
time = 0
while len(data) != 0:
	if data[0][1] <= q:
		time += data[0][1]
		print(f'{data[0][0]} {time}')
		data.pop(0)
	else:
		time += q
		data[0][1] -= q
		data.append(data.pop(0))


from collections import deque
(n, q) = map(int, input().split())
queue = deque()
for i in range(n):
	(N, T) = input().split()
	queue.append([N, int(T)])
t = 0
while queue:
	x = queue.popleft()
	if x[1] <= q:
		t += x[1]
		print(x[0], t)
	else:
		queue.append([x[0], x[1] - q])
		t += q


(N, Q) = map(int, input().strip().split())
A = []
D = {}
for l in range(N):
	(n, t) = input().strip().split()
	A.append([n, int(t)])
T = 0
while A:
	l = A.pop(0)
	if l[1] > Q:
		l[1] -= Q
		T += Q
		A.append(l)
	else:
		T += l[1]
		print(l[0], T)


(n, q) = list(map(int, input().split()))
que = []
for _ in range(n):
	(name, time) = input().split()
	que.append([name, int(time)])
elapsed_time = 0
while que:
	(name, time) = que.pop(0)
	dt = min(q, time)
	time -= dt
	elapsed_time += dt
	if time > 0:
		que.append([name, time])
	else:
		print(name, elapsed_time)


answer = []
(n, q) = [int(i) for i in input().split(' ')]
for i in range(n):
	(name, time) = input().split(' ')
	time = int(time)
	answer.append([name, time])
time = 0
while len(answer) > 0:
	p = answer.pop(0)
	if p[1] <= q:
		time += p[1]
		print(p[0], time)
	else:
		p[1] -= q
		time += q
		answer.append(p)


from collections import deque
(n, q) = map(int, input().split())
p = deque()
for _ in range(n):
	p_i = input().split()
	p.append([p_i[0], int(p_i[1])])
time = 0
while p:
	if p[0][1] > q:
		p[0][1] -= q
		time += q
		p.append(p.popleft())
	else:
		time += p[0][1]
		print(f'{p[0][0]} {time}')
		p.popleft()


from collections import deque
(n, q) = map(int, input().split())
ans = 0
NT = [list(input().split()) for i in range(n)]
queue = deque(NT)
while len(queue) > 0:
	a = queue.popleft()
	if int(a[1]) <= q:
		ans += int(a[1])
		print(a[0], ans)
	else:
		ans += q
		a[1] = str(int(a[1]) - q)
		queue.append(a)


from collections import deque
(n, q) = map(int, input().split())
queue = deque()
for i in range(n):
	(name, time) = input().split()
	queue.append((name, int(time)))
t = 0
while queue:
	(name, time) = queue.popleft()
	t += min(q, time)
	if time > q:
		queue.append((name, time - q))
	else:
		print(name, t)


from collections import deque
d = deque()
time = 0
(n, q) = map(int, input().split())
for i in range(n):
	d.append(input().split())
while len(d):
	d[0][1] = int(d[0][1])
	if d[0][1] > q:
		d[0][1] -= q
		time += q
		d.rotate(-1)
	else:
		time += d[0][1]
		print(f'{d.popleft()[0]} {time}')


from collections import deque
(n, q) = map(int, input().split())
queue = deque()
for _ in range(n):
	(name, t) = input().split()
	t = int(t)
	queue.append((name, t))
time = 0
while len(queue) != 0:
	(name, t) = queue.popleft()
	if t > q:
		queue.append((name, t - q))
		time += q
	elif t <= q:
		time += t
		print(name, time)


(N, q) = map(int, input().split())
job = {}
for i in range(N):
	(name, t) = input().split()
	job[name] = int(t)
from collections import deque
Q = deque(job.keys())
time = 0
while len(Q):
	cur = Q.popleft()
	if job[cur] > q:
		job[cur] -= q
		Q.append(cur)
		time += q
	else:
		time += job[cur]
		print('{} {}'.format(cur, time))


from collections import deque
(n, q) = map(int, input().split())
l = deque((list(map(str, input().split())) for _ in range(n)))
t = 0
while l:
	(a, b) = l.popleft()
	b = int(b)
	if b <= q:
		t += b
		print(a, t)
	else:
		b -= q
		t += q
		l.append([a, b])


(n, q) = map(int, input().split())
task = []
for _ in range(n):
	s = input().split()
	task.append([s[0], int(s[1])])
ans = 0
while len(task):
	t = task.pop(0)
	if t[1] - q <= 0:
		ans += t[1]
		print(t[0], ans)
	else:
		ans += q
		task.append([t[0], t[1] - q])


(n, q, *S) = open(0).read().split()
q = int(q)
J = list(zip(*[iter(S)] * 2))
T = 0
while J:
	(n, t) = J.pop(0)
	t = int(t)
	if t <= q:
		T += t
		print(f'{n} {T}')
	else:
		T += q
		J.append((n, t - q))


import collections
que = collections.deque()
(n, q) = map(int, input().split())
T = 0
for i in range(n):
	que.append(list(input().split()))
while len(que) > 0:
	A = que.popleft()
	if int(A[1]) > q:
		A[1] = str(int(A[1]) - q)
		que.append(A)
		T += q
	else:
		T += int(A[1])
		print(A[0], T)


from collections import deque
(n, q) = map(int, input().split())
que = deque([])
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
t = 0
while len(que) > 0:
	atop = que.popleft()
	spend = min(atop[1], q)
	atop[1] -= spend
	t += spend
	if atop[1] == 0:
		print(atop[0], t)
	else:
		que.append(atop)


from collections import deque
(n, p) = map(int, input().split())
que = deque([])
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
t = 0
while len(que) > 0:
	atop = que.popleft()
	spend = min(atop[1], p)
	atop[1] -= spend
	t += spend
	if atop[1] == 0:
		print(atop[0], t)
	else:
		que.append(atop)


(n, q) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(input().split()))
	a[i][1] = int(a[i][1])
time = 0
while a:
	if a[0][1] > q:
		time += q
		a[0][1] -= q
		a.append([a[0][0], a[0][1]])
		del a[0]
	else:
		time += a[0][1]
		print(a[0][0] + ' ' + str(time))
		del a[0]


from collections import deque
(n, qt) = map(int, input().strip().split())
q = deque()
for i in range(n):
	(name, time) = input().strip().split()
	q.append((name, int(time)))
t = 0
while q:
	(name, time) = q.popleft()
	if time <= qt:
		t += time
		print(name, t)
	else:
		t += qt
		time -= qt
		q.append((name, time))


(n, q) = map(int, input().split())
name = []
time = []
for i in range(n):
	a = input().split()
	name.append(a[0])
	time.append(int(a[1]))
s = 0
while name:
	if time[0] > q:
		time[0] -= q
		time.append(time.pop(0))
		name.append(name.pop(0))
		s += q
	else:
		s += time[0]
		time.pop(0)
		print(name.pop(0), s)


(N, Q) = map(int, input().split())
A = [input().split() for _ in range(N)]
time = 0
while True:
	a = A.pop(0)
	n = a[0]
	v = int(a[1]) - Q
	if v <= 0:
		time += Q + v
		print(f'{n} {time}')
	else:
		A.append([n, v])
		time += Q
	if len(A) <= 0:
		break


from collections import deque
(n, q) = map(int, input().split())
d = deque()
(e, a) = (0, [])
for _ in range(n):
	(m, t) = input().split()
	d.append((m, int(t)))
while d:
	(m, t) = d.popleft()
	if t - q > 0:
		e += q
		d.append((m, t - q))
	else:
		e += t
		a += [f'{m} {e}']
print('\n'.join(a))


(N, q) = list(map(int, input().split(' ')))
que = []
time = 0
for _ in range(N):
	(t1, t2) = input().split(' ')
	que.append((t1, int(t2)))
while len(que) != 0:
	task = que.pop(0)
	if task[1] > q:
		ntask = (task[0], task[1] - q)
		que.append(ntask)
		time += q
	else:
		print(task[0], time + task[1])
		time += task[1]


from collections import deque
(n, q) = map(int, input().split())
deq = deque()
for i in range(n):
	(name, time) = input().split()
	deq.append((name, int(time)))
sum = 0
while deq:
	(name, time) = deq.popleft()
	if time <= q:
		sum += time
		print(name, sum)
	else:
		sum += q
		deq.append((name, time - q))


(n, q) = list(map(int, input().split()))
n_t = [input().split() for i in range(n)]
p_time = 0
while n_t:
	if int(n_t[0][1]) <= q:
		p_time += int(n_t[0][1])
		finish = n_t.pop(0)
		print(finish[0], p_time)
	else:
		n_t[0][1] = str(int(n_t[0][1]) - q)
		n_t.append(n_t[0])
		p_time += q
		del n_t[0]


import sys
from collections import deque
(n, q) = map(int, input().split())
f = lambda n, t: (n, int(t))
queue = deque((f(*l.split()) for l in sys.stdin))
t = 0
while queue:
	(name, time) = queue.popleft()
	t += min(q, time)
	if time > q:
		queue.append((name, time - q))
	else:
		print(name, t)


from collections import deque
(n, q) = map(int, input().split())
x = []
for i in range(0, n):
	x.append(list(input().split()))
	x[-1][1] = int(x[-1][1])
que = deque(x)
time = 0
while len(que) > 0:
	y = que.popleft()
	if y[1] <= q:
		time = time + y[1]
		print(y[0], time)
	else:
		time = time + q
		y[1] = y[1] - q
		que.append(y)


(n, q) = map(int, input().split())
A = [list(map(str, input().split())) for i in range(n)]
time = 0
flag = 1
while len(A) > 0:
	if int(A[0][1]) <= q:
		time += int(A[0][1])
		print(A[0][0], time)
		del A[0]
	else:
		time += q
		A[0][1] = str(int(A[0][1]) - q)
		A.append(A[0])
		del A[0]


def parse(l):
	(name, time) = l.split()
	return (name, int(time))
(n, q) = map(int, input().split())
queue = list((parse(input()) for _ in range(n)))
time = 0
while queue:
	(name, remain_time) = queue.pop(0)
	if remain_time > q:
		time += q
		queue.append((name, remain_time - q))
	else:
		time += remain_time
		print(name, time)


info = list(map(int, input().split()))
p_name = []
p_time = []
time = 0
for i in range(info[0]):
	input_pro = input().split()
	p_name.append(input_pro[0])
	p_time.append(int(input_pro[1]))
while len(p_time) != 0:
	nt = p_time.pop(0)
	nn = p_name.pop(0)
	if nt <= info[1]:
		time += nt
		print(nn + ' ' + str(time))
	else:
		p_name.append(nn)
		p_time.append(nt - info[1])
		time += info[1]


import sys
from collections import deque
s = sys.stdin.readlines()
q = int(s[0].split()[1])
f = lambda x, y: (x, int(y))
d = deque((f(*e.split()) for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


from collections import deque
dq = deque()
(n, q) = map(int, input().split())
for _ in range(n):
	(name, time) = input().split()
	dq.append((name, int(time)))
t = 0
while dq:
	(name, time) = dq.popleft()
	if time > q:
		dq.append((name, time - q))
		t += q
	else:
		t += time
		print(name, t)


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
t = 0
i = 0
while queue:
	i %= len(queue)
	p = queue[i]
	t += min(q, p[1])
	if p[1] > q:
		p[1] -= q
		i += 1
	else:
		print(p[0], t)
		queue.pop(i)


import sys
from collections import deque
s = sys.stdin.readlines()
q = int(s[0].split()[1])
f = lambda x, y: (x, int(y))
d = deque((f(*e.split()) for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(name, time) = map(str, input().split())
	queue.append([name, int(time)])
time = 0
while len(queue) != 0:
	p = queue.pop(0)
	if p[1] <= q:
		time += p[1]
		print(p[0], time)
	else:
		time += q
		p[1] -= q
		queue.append(p)


from collections import deque
(n, q) = map(int, input().split())
Queue = deque()
for _ in range(n):
	Queue.append(input())
ans = 0
while len(Queue) > 0:
	x = Queue.popleft()
	(name, time) = x.split()
	t = int(time)
	if t > q:
		ans += q
		Queue.append(name + ' ' + str(t - q))
	else:
		ans += t
		print(name, ans)


(n, q) = map(int, input().split())
A = []
for _ in range(n):
	(name, time) = input().split()
	A += [[name, int(time)]]
progress = 0
while A:
	if A[0][1] <= q:
		progress += A[0][1]
		print(A[0][0], progress)
		A.pop(0)
	else:
		progress += q
		A[0][1] -= q
		A += [A[0]]
		A.pop(0)


from queue import Queue
(n, q) = map(int, input().split())
que = Queue()
for i in range(n):
	(name, time) = input().split()
	que.put((name, int(time)))
total = 0
while que.qsize() > 0:
	(name, time) = que.get()
	total += min(q, time)
	if q < time:
		que.put((name, time - q))
	else:
		print(name, total)


n_quant = [int(t) for t in input().split()]
que = []
for i in range(n_quant[0]):
	tmp = input().split()
	que.append((tmp[0], int(tmp[1])))
time = 0
while len(que) > 0:
	(name, val) = que.pop(0)
	if val > n_quant[1]:
		que.append((name, val - n_quant[1]))
		time += n_quant[1]
	else:
		time += val
		print(name, time)


from collections import *
q = deque([])
(a, b) = map(int, input().split())
for i in range(a):
	(c, d) = input().split()
	q.append([c, int(d)])
t = 0
while len(q) > 0:
	tmp = q.popleft()
	if tmp[1] > b:
		tmp[1] -= b
		q.append(tmp)
		t += b
	else:
		print(tmp[0], tmp[1] + t)
		t += tmp[1]


from queue import Queue
(n, q) = map(int, input().split())
que = Queue()
for i in range(n):
	(name, time) = input().split()
	que.put((name, int(time)))
total = 0
while que.qsize() > 0:
	(name, time) = que.get()
	total += min(q, time)
	if q < time:
		que.put((name, time - q))
	else:
		print(name, total)


(n, q) = [int(s) for s in input().split()]
queue = []
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
time = 0
while queue:
	processing = queue.pop(0)
	t = min(processing[1], q)
	time += t
	processing[1] -= t
	if processing[1] == 0:
		print(processing[0], time)
	else:
		queue.append(processing)


(n, q) = map(int, input().split())
que = []
for _ in range(n):
	(p, t) = input().split()
	que.append([p, int(t)])
now = 0
while len(que) != 0:
	if que[0][1] > q:
		que[0][1] -= q
		now += q
		que.append(que.pop(0))
	else:
		now += que[0][1]
		print('{} {}'.format(que[0][0], now))
		que.pop(0)


from collections import deque
(n, q) = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque((f(*input().split()) for _ in range(n)))
ans = 0
li = []
while dq:
	(s, t) = dq.popleft()
	if t <= q:
		ans += t
		li.append(s + ' ' + str(ans))
	else:
		ans += q
		dq.append((s, t - q))
print(*li, sep='\n')


import sys
input = sys.stdin.readline
from collections import deque
(n, q) = map(int, input().split())
que = deque()
for _ in range(n):
	(x, y) = input().split()
	que.append([x, int(y)])
time = 0
while len(que) != 0:
	(x, y) = que.popleft()
	if y <= q:
		time += y
		print(x, time)
	else:
		y -= q
		que.append([x, y])
		time += q


from collections import deque
P = deque()
t = 0
(n, q) = map(int, input().split())
for i in range(n):
	(name, time) = input().split()
	P.append([name, int(time)])
while len(P):
	if P[0][1] <= q:
		t += P[0][1]
		print(P[0][0], t)
		P.popleft()
	else:
		t += q
		P[0][1] -= q
		P.rotate(-1)


from collections import deque
(n, q) = map(int, input().split())
q = int(q)
queue = deque()
for i in range(n):
	queue.append(list(input().split()))
time = 0
while len(queue) > 0:
	tmp = queue.popleft()
	if int(tmp[1]) <= q:
		print(' '.join([tmp[0], str(time + int(tmp[1]))]))
		time += int(tmp[1])
	else:
		queue.append([tmp[0], int(tmp[1]) - q])
		time += q


(n, quantum) = list(map(int, input().split()))
queue = []
sumOfTime = 0
for _ in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
while len(queue) > 0:
	(name, time) = queue.pop(0)
	if time > quantum:
		sumOfTime += quantum
		queue.append([name, time - quantum])
	else:
		sumOfTime += time
		print(name, sumOfTime)


(n, q) = map(int, input().split())
que = []
for i in range(n):
	(p, t) = map(str, input().split())
	que.append([p, int(t)])
total = 0
while que != []:
	pt = que.pop(0)
	if pt[1] <= q:
		total += pt[1]
		print(pt[0], total)
	else:
		total += q
		que.append([pt[0], pt[1] - q])


from collections import deque

def solve(n, t):
	q = deque([])
	for i in range(n):
		l = list(input().split())
		q.append(l)
	time = 0
	while not len(q) == 0:
		x = q.popleft()
		if int(x[1]) - t > 0:
			x[1] = str(int(x[1]) - t)
			time += t
			q.append(x)
		else:
			print(x[0] + ' ' + str(time + int(x[1])))
			time += int(x[1])
(n, t) = map(int, input().split())
solve(n, t)


(n, q) = map(int, input().split())
queue = [input() for i in range(n)]
cnt = 0
while len(queue) != 0:
	(name, time) = queue.pop(0).split()
	if int(time) <= q:
		cnt += int(time)
		print(name, str(cnt))
	else:
		cnt += q
		time = str(int(time) - q)
		queue.append(name + ' ' + time)


import collections
(n, q) = map(int, input().split())
P = collections.deque()
for _ in range(n):
	(name, time) = input().split()
	P.append([name, int(time)])
total_time = 0
while P:
	(name, time) = P.popleft()
	if time <= q:
		total_time += time
		print(name, total_time)
	else:
		time -= q
		total_time += q
		P.append([name, time])


(n, q) = [int(s) for s in input().split()]
queue = []
for i in range(n):
	(name, time) = input().split()
	queue.append((name, int(time)))
total = 0
while len(queue) > 0:
	(name, time) = queue.pop(0)
	if time > q:
		total += q
		queue.append((name, time - q))
	else:
		total += time
		print(name, total)


import heapq
(N, Q) = map(int, input().split())
nati = []
for i in range(N):
	(na, ti) = input().split()
	nati.append([str(na), int(ti)])
now = 0
while nati:
	(na, ti) = nati.pop(0)
	if ti > Q:
		ti -= Q
		nati.append([na, ti])
		now += Q
	else:
		now += ti
		print(na, now)


from collections import deque
dq = deque()
(n, q) = map(int, input().split())
for i in range(n):
	(p, t) = map(str, input().split())
	dq.append([p, int(t)])
t = 0
while dq:
	if dq[0][1] <= q:
		t += dq[0][1]
		print(str(dq[0][0]) + ' ' + str(t))
		dq.popleft()
	else:
		t += q
		dq[0][1] -= q
		dq.append(dq[0])
		dq.popleft()


(n, q) = map(int, input().split())
A = []
for i in range(n):
	A.append(input())
i = 0
time = 0
while not A == []:
	(x, y) = map(str, A[i].split())
	y = int(y)
	if y <= q:
		time += y
		times = str(time)
		A[i] = x + ' ' + times
		print(A.pop(i))
	else:
		z = y - q
		z = str(z)
		time += q
		A.append(x + ' ' + z)
		del A[i]


from collections import deque
(n, q) = map(int, input().split())
procs = deque(([name, int(time)] for (name, time) in [input().split() for _ in range(n)]))
tot_time = 0
while procs:
	(name, time) = procs.popleft()
	if time <= q:
		tot_time += time
		print(name, tot_time)
	else:
		tot_time += q
		procs.append([name, time - q])


(n, t) = map(int, input().split())
Task = []
for i in range(n):
	(nm, le) = map(str, input().split())
	Task.append([nm, le])
T = 0
while n > 0:
	k = Task.pop(0)
	k[1] = int(k[1])
	if k[1] > t:
		k[1] = k[1] - t
		T = T + t
		Task.append(k)
		n = len(Task)
	else:
		T = T + k[1]
		k[1] = T
		print(' '.join(map(str, k)))
		n = len(Task)


(n, q) = map(int, input().split())
Que = []
for _ in range(n):
	(name, time) = input().split()
	Que.append([name, int(time)])
elps = 0
while Que != []:
	tmp = Que.pop(0)
	if tmp[1] - q > 0:
		tmp[1] = tmp[1] - q
		Que.append(tmp)
		elps += q
	else:
		elps += tmp[1]
		print(tmp[0], elps)


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(p, t) = map(str, input().split())
	t = int(t)
	queue.append([p, t])
total_time = 0
while len(queue) > 0:
	x = queue.pop(0)
	if x[1] > q:
		total_time += q
		x[1] -= q
		queue.append(x)
	else:
		total_time += x[1]
		print(x[0], total_time)


import collections
q = collections.deque()
(n, m) = map(int, input().split())
for i in range(n):
	(a, b) = input().split()
	b = int(b)
	q.append([a, b])
t = 0
while len(q) != 0:
	n = q.popleft()
	if n[1] <= m:
		t += n[1]
		print(n[0], str(t))
	else:
		t += m
		n[1] -= m
		q.append(n)


(n, q) = map(int, input().split())
tasks = []
for _ in range(n):
	(ni, ti) = input().split()
	tasks.append((ni, int(ti)))
t = 0
while len(tasks) > 0:
	(ni, ti) = tasks.pop(0)
	if ti <= q:
		t += ti
		print(f'{ni} {t}')
	else:
		tasks.append((ni, ti - q))
		t += q


(n, q) = list(map(int, input().split()))
P = [[p, int(t)] for (p, t) in [input().split() for _ in range(n)]]
time = 0
while len(P) > 0:
	pi = P.pop(0)
	if pi[1] > q:
		P.append([pi[0], pi[1] - q])
		time += q
	else:
		time += pi[1]
		print(pi[0], time)


(n, q) = map(int, input().split())
p = [input().split() for _ in range(n)]
t = i = 0
while n:
	i = i % n
	if int(p[i][1]) <= q:
		t += int(p[i][1])
		print(p[i][0], t)
		del p[i]
		n -= 1
	else:
		p[i][1] = int(p[i][1]) - q
		t += q
		i += 1


import sys
from collections import deque
s = sys.stdin.readlines()
q = int(s[0].split()[1])
d = deque((list(e.split()) for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	v = int(v)
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


from queue import Queue
(n, q) = map(int, input().split())
qu = Queue()
for i in range(n):
	(name, t) = input().split()
	qu.put((name, int(t)))
cnt = 0
while qu.qsize() > 0:
	(name, t) = qu.get()
	cnt += min(q, t)
	if q < t:
		qu.put((name, t - q))
	else:
		print(name, cnt)


from collections import deque
(n, q) = map(int, input().split())
name_time = [input().split() for _ in range(n)]
d = deque()
d.extend(name_time)
total_time = 0
while d:
	(name, time) = d.popleft()
	time = int(time)
	if time <= q:
		total_time += time
		print(name, total_time)
	else:
		total_time += q
		d.append([name, str(time - q)])


(n, q) = [int(val) for val in input().split(' ')]
ps = [0] * n
t = [0] * n
for i in range(n):
	(ps[i], t[i]) = input().split(' ')
output = []
qsum = 0
while t:
	psi = ps.pop(0)
	ti = int(t.pop(0))
	if ti <= q:
		qsum += ti
		output.append(psi + ' ' + str(qsum))
	else:
		t.append(ti - q)
		ps.append(psi)
		qsum += q
print('\n'.join(output))


(n, q) = map(int, input().split())
total_time = 0
queue = [input().split() for i in range(n)]
while queue:
	ps = queue.pop(0)
	time = int(ps[1])
	if time <= q:
		total_time += time
		print(ps[0], total_time)
	else:
		total_time += q
		time -= q
		ps[1] = time
		queue.append(ps)


from collections import deque
(n, q_time) = map(int, input().split())
d = deque()
for i in range(n):
	(x, y) = input().split()
	d.append((x, int(y)))
total = 0
while d:
	(name, time) = d.popleft()
	if time <= q_time:
		total += time
		print(name, total)
	else:
		total += q_time
		time -= q_time
		d.append((name, time))


def readInts():
	return [int(i) for i in input().split()]

def readLine(n: int):
	return [input().split() for _ in range(n)]
(n, q) = readInts()
ps = readLine(n)
t = 0
while ps != []:
	p = ps.pop(0)
	pt = int(p[1])
	if pt <= q:
		t += pt
		print(p[0], t)
	else:
		t += q
		ps.append([p[0], pt - q])


from collections import deque
(n, q) = map(int, input().split())
dq = deque()
for _ in range(n):
	(p, t) = map(str, input().split())
	dq.append([p, int(t)])
total = 0
while dq:
	pt = dq.popleft()
	if pt[1] <= q:
		total += pt[1]
		print(pt[0], total)
	else:
		total += q
		dq.append([pt[0], pt[1] - q])


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	queue.append(input().split())
wholetime = 0
while len(queue) != 0:
	target = queue.pop(0)
	time = int(target[1])
	if time <= q:
		wholetime += time
		print(target[0], wholetime)
	else:
		wholetime += q
		queue.append([target[0], time - q])


(n, q) = map(int, input().split())
processes = [[li[0], int(li[1])] for li in [input().split() for _ in range(n)]]
elapsed_time = 0
while len(processes):
	process = processes.pop(0)
	if process[1] > q:
		process[1] -= q
		processes.append(process)
		elapsed_time += q
	else:
		elapsed_time += process[1]
		print(process[0], elapsed_time)


(n, q) = map(int, input().split())
que = []
t = 0
for i in range(n):
	que.append(input().split())
while len(que) != 0:
	proc = que.pop(0)
	if int(proc[1]) <= q:
		t += int(proc[1])
		print(proc[0], t)
	else:
		t += q
		proc[1] = int(proc[1]) - q
		que.append(proc)


import queue
(N, Q) = map(int, input().split())
P = queue.Queue()
for _ in range(N):
	p = input().split()
	P.put([p[0], int(p[1])])
time = 0
while not P.empty():
	p = P.get()
	if p[1] <= Q:
		time += p[1]
		print(p[0], time)
	else:
		time += Q
		P.put([p[0], p[1] - Q])


(n, ms) = map(int, input().split())
a = [list(map(str, input().split())) for _ in range(n)]
total = 0
for i in a:
	tmp = int(i[1]) - ms
	if tmp <= 0:
		total += ms + tmp
		print('{} {}'.format(i[0], total))
	else:
		total += ms
		a.append([i[0], tmp])


import queue
(n, q) = map(int, input().split())
queue = queue.Queue()
for i in range(n):
	(name, time) = input().split()
	queue.put((name, int(time)))
t = 0
while not queue.empty():
	(name, time) = queue.get()
	if time <= q:
		t += time
		print(name, t)
	else:
		queue.put((name, time - q))
		t += q


(a, b) = map(int, input().split())
list = [[0 for i in range(2)] for j in range(a)]
for i in range(a):
	list[i] = input().split()
n = 0
s = 0
while n != a:
	if int(list[n][1]) > b:
		list[n][1] = int(list[n][1]) - b
		list.append(list[n])
		list.pop(n)
		s += b
	else:
		print(list[n][0], s + int(list[n][1]))
		s += int(list[n][1])
		n += 1


(n, quantum) = list(map(int, input().split()))
queue = []
sumOfTime = 0
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
while len(queue) > 0:
	(name, time) = queue.pop(0)
	if time > quantum:
		sumOfTime += quantum
		queue.append([name, time - quantum])
	else:
		sumOfTime += time
		print(name, sumOfTime)


import collections
(n, q) = map(int, input().split())
queue = collections.deque(maxlen=100000)
for i in range(n):
	(p, t) = input().split()
	queue.append([p, int(t)])
time = 0
while queue:
	(p, t) = queue.popleft()
	if t <= q:
		time += t
		print(p, time)
	else:
		time += q
		queue.append([p, t - q])


from collections import deque
(n, q) = map(int, input().split())
queue = deque([])
for i in range(n):
	a = input().split()
	queue.append([a[0], int(a[1])])
time = 0
while queue:
	b = queue.popleft()
	if b[1] <= q:
		time += b[1]
		print(b[0] + ' ' + str(time))
	else:
		b[1] -= q
		time += q
		queue.append(b)


import sys
from collections import deque
s = sys.stdin.readlines()
q = int(s[0].split()[1])
f = lambda x, y: (x, int(y))
d = deque((f(*e.split()) for e in s[1:]))
(t, a) = (0, [])
while d:
	(k, v) = d.popleft()
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		a += [f'{k} {t}']
print(*a, sep='\n')


from collections import deque
(n, q) = map(int, input().split())
Q = deque()
for i in range(n):
	a = input().split()
	Q.append([a[0], int(a[1])])
s = 0
while Q:
	qt = Q.popleft()
	if qt[1] > q:
		Q.append([qt[0], qt[1] - q])
		s += q
	else:
		s += qt[1]
		print(qt[0], s)


from collections import deque
(n, m) = map(int, input().split())
name = [''] * n
time = [''] * n
for i in range(n):
	(name[i], time[i]) = input().split()
time = list(map(int, time))
Q = deque([i for i in range(n)])
t = 0
while Q != deque([]):
	q = Q.popleft()
	if time[q] <= m:
		t += time[q]
		print(name[q] + ' ' + str(t))
	else:
		t += m
		time[q] -= m
		Q.append(q)


from collections import deque
(n, q) = map(int, input().split())
dq = deque()
for _ in range(n):
	(name, time) = input().split()
	dq.append([name, int(time)])
cnt = 0
while dq:
	qt = dq.popleft()
	if qt[1] <= q:
		cnt += qt[1]
		print(qt[0], cnt)
	else:
		cnt += q
		qt[1] -= q
		dq.append(qt)


from collections import deque
(n, q) = map(int, input().split())
a = deque([])
for i in range(n):
	a.append(list(input().split()))
end_time = 0
while len(a):
	e = a.popleft()
	if int(e[1]) - q > 0:
		e[1] = int(e[1]) - q
		a.append(e)
		end_time += q
	else:
		end_time += int(e[1])
		print(e[0], end_time)


from collections import deque
(n, q) = map(int, input().split())
queue = deque([input().split() for _ in range(n)])
t = 0
while len(queue):
	p = queue.popleft()
	if int(p[1]) <= q:
		t += int(p[1])
		print(p[0], t)
	else:
		p[1] = int(p[1]) - q
		t += q
		queue.append(p)


from collections import deque
(n, q) = map(int, input().split())
l = [input().split() for _ in range(n)]
d = deque()
for i in l:
	d.append((i[0], int(i[1])))
t = 0
while d:
	x = d.popleft()
	if x[1] <= q:
		print(x[0], t + x[1])
		t = t + x[1]
	else:
		t += q
		d.append((x[0], x[1] - q))


import sys
from collections import deque
s = sys.stdin.readlines()
(n, q) = map(int, s[0].split())
d = deque((e.split() for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	v = int(v)
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


from collections import deque
(n, q) = list(map(int, input().split()))
que = deque([])
for i in range(n):
	(n, t) = input().split()
	que.append((n, t))
e = 0
while len(que) > 0:
	(n, t) = que.popleft()
	if int(t) <= q:
		e += int(t)
		print(n, e)
	else:
		e += q
		que.append((n, int(t) - q))


from collections import deque
(n, q) = map(int, input().split())
Q = deque()
for i in range(n):
	(name, time) = input().split()
	Q.append([name, int(time)])
sum = 0
while Q:
	qt = Q.popleft()
	if qt[1] <= q:
		sum += qt[1]
		print(qt[0], sum)
	else:
		sum += q
		qt[1] -= q
		Q.append(qt)


from collections import deque
(n, q) = map(int, input().split(' '))
d = deque()
for _ in range(n):
	l = list(input().split(' '))
	l[1] = int(l[1])
	d.appendleft(l)
_q = q
s = 0
while len(d) > 0:
	e = d.pop()
	if e[1] - _q > 0:
		s += _q
		e[1] -= _q
		d.appendleft(e)
	else:
		s += e[1]
		print(e[0], s)
	_q = q


from queue import Queue
q = Queue()
Q = []
time = 0
N = list(map(int, input().split()))
qt = N[1]
for i in range(N[0]):
	Q.append(list(map(str, input().split())))
for i in range(N[0]):
	q.put(Q[i])
while q.empty() == False:
	i = q.get()
	i[1] = str(int(i[1]) - qt)
	if int(i[1]) <= 0:
		time = time + int(i[1]) + qt
		print(i[0], time)
		pass
	else:
		time = time + qt
		q.put(i)


from collections import deque
(pnum, mtime) = map(int, input().split(' '))
q = deque([])
[q.append(input().split(' ')) for u in range(pnum)]
tcnt = 0
while len(q) > 0:
	qq = q.popleft()
	ztime = int(qq[1]) - mtime
	if ztime <= 0:
		tcnt += int(qq[1])
		print(qq[0], int(tcnt))
	else:
		q.append([qq[0], ztime])
		tcnt += mtime


import sys
input = sys.stdin.readline
from collections import deque
(n, q) = map(int, input().split())
nt = [list(input().split()) for _ in range(n)]
t = 0
l = deque(nt)
while l:
	tmp = l.popleft()
	tmp[1] = int(tmp[1])
	if tmp[1] > q:
		tmp[1] -= q
		l.append(tmp)
		t += q
	else:
		t += tmp[1]
		print(tmp[0], t)


from collections import deque
nq = list(map(int, input().split()))
n = nq[0]
q = nq[1]
t = 0
queue = deque()
for i in range(n):
	p = input().split()
	queue.append(p)
while queue:
	proc = queue.popleft()
	if int(proc[1]) <= q:
		t += int(proc[1])
		print(proc[0], t)
	else:
		proc[1] = int(proc[1]) - q
		t += q
		queue.append(proc)


(n, q) = map(int, input().split())
process = [input().split() for _ in range(n)]
time = 0
while process:
	p = process.pop(0)
	if int(p[1]) <= q:
		time += int(p[1])
		print(p[0], time)
	else:
		time += q
		process.append([p[0], int(p[1]) - q])


time = 0
overtime = 0
(n, q) = map(int, input().split())
L = [input().split() for i in range(n)]
while True:
	if len(L) == 0:
		break
	else:
		s = L.pop(0)
		s[1] = int(s[1])
		if s[1] > q:
			s[1] = s[1] - q
			time += q
			L.append(s)
		else:
			time += s[1]
			print(s[0], time)


(n, q) = list(map(int, input().split()))
p = [0] * n
t = [0] * n
for i in range(n):
	(p[i], t[i]) = list(input().split())
i = 0
sum_t = 0
while 1:
	if i == len(t):
		break
	tmp_t = int(t[i]) - q
	if tmp_t <= 0:
		sum_t += int(t[i])
		print(p[i], sum_t)
	else:
		sum_t += q
		p.append(p[i])
		t.append(tmp_t)
	i += 1


process = []
(n, q) = [int(i) for i in input().split(' ')]
for i in range(n):
	(name, time) = input().split(' ')
	time = int(time)
	process.append([name, time])
time = 0
while len(process) > 0:
	p = process.pop(0)
	if p[1] <= q:
		time += p[1]
		print(p[0], time)
	else:
		p[1] -= q
		time += q
		process.append(p)


(n, q) = map(int, input().split())
prcs = []
for _ in range(n):
	(nm, tm) = input().split()
	prcs.append([nm, int(tm)])
elps = 0
while prcs != []:
	tmp = prcs.pop(0)
	if tmp[1] - q > 0:
		tmp[1] = tmp[1] - q
		prcs.append(tmp)
		elps += q
	else:
		elps += tmp[1]
		print(tmp[0], elps)


from collections import deque
(n, q) = map(int, input().split())
a = deque((list(input().split()) for _ in range(n)))
total = 0
while a:
	b = a.popleft()
	b[1] = int(b[1])
	if b[1] > q:
		b[1] -= q
		a.append(b)
		total += q
	else:
		total += b[1]
		b[1] = total
		print(*b)


from collections import deque
(n, q) = map(int, input().split())
que = deque()
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
t = 0
while len(que) > 0:
	que_t = que.popleft()
	if que_t[1] > q:
		que_t[1] -= q
		t += q
		que.append(que_t)
	else:
		t += que_t[1]
		print(que_t[0], t)


from collections import deque
(n, q) = list(map(int, input().split()))
nt = deque()
for i in range(n):
	nt.append(input().split())
	nt[i][1] = int(nt[i][1])
t = 0
while len(nt) > 0:
	if nt[0][1] <= q:
		t += nt[0][1]
		print(str(nt[0][0]) + ' ' + str(t))
		nt.popleft()
	else:
		nt[0][1] = nt[0][1] - q
		nt.rotate(-1)
		t += q


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(name, time) = map(str, input().split())
	queue.append([name, int(time)])
length = 0
while queue:
	num = queue[0]
	if num[1] <= q:
		length += num[1]
		queue.pop(0)
		print(num[0], length)
	else:
		length += q
		queue.pop(0)
		queue.append([num[0], int(num[1] - q)])


import collections
dq = collections.deque()
(N, Q) = map(int, input().split())
for i in range(N):
	(name, time) = input().split()
	dq.append((name, int(time)))
total_time = 0
while dq:
	(name, time) = dq.popleft()
	total_time += min(Q, time)
	if time <= Q:
		print(name, total_time)
	else:
		dq.append((name, time - Q))


(pnum, mtime) = map(int, input().split(' '))
total = [list(input().split(' ')) for _ in range(pnum)]
cnt = 0
tcnt = 0
while len(total) > 0:
	ztime = int(total[0][1]) - mtime
	if ztime <= 0:
		tcnt += int(total[0][1])
		print(total[0][0], int(tcnt))
		total.pop(0)
	else:
		total.append([total[0][0], ztime])
		total.pop(0)
		tcnt += mtime
	cnt += 1


from collections import deque
(n, q) = map(int, input().split())
ps = [input().split() for _ in range(n)]
que = deque(ps)
time = 0
while que:
	(p, rest) = que.popleft()
	elapsed = min(q, int(rest))
	time += elapsed
	rest = str(int(rest) - elapsed)
	if int(rest) > 0:
		que.append([p, rest])
	else:
		print(p, time)


import collections
d = collections.deque([])
(n, p) = map(int, input().split())
for i in range(n):
	(name, t) = input().split()
	d.append((name, int(t)))
tt = 0
while len(d) > 0:
	(name, t) = d.popleft()
	if t <= p:
		tt += t
		print('{0} {1}'.format(name, tt))
	elif t > p:
		t = t - p
		tt += p
		d.append((name, t))


(n, p) = map(int, input().split())
ps = []
for i in range(n):
	ps.append(input().split())
	ps[i][1] = int(ps[i][1])
t = 0
while len(ps) > 0:
	if ps[0][1] > p:
		ps.append([ps[0][0], ps[0][1] - p])
		t += p
		ps.pop(0)
	else:
		t += ps[0][1]
		print('{0} {1}'.format(ps[0][0], t))
		ps.pop(0)


import sys, collections
p = [s.split() for s in sys.stdin]
p = collections.deque([[s[0], int(s[1])] for s in p])
q = int(p.popleft()[1])
t = 0
while len(p) > 0:
	if p[0][1] > q:
		p[0][1] -= q
		p += [p.popleft()]
		t += q
	else:
		t += p[0][1]
		print(p.popleft()[0], t)


(n, q) = map(int, input().split())
queue = []
elaps = 0
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
while len(queue) > 0:
	u = queue.pop(0)
	c = min(q, u[1])
	u[1] -= c
	elaps += c
	if u[1] > 0:
		queue.append(u)
	else:
		print(u[0], elaps)


from collections import deque
(N, q) = map(int, input().split())
A = [list(map(str, input().split())) for i in range(N)]
A = [[A[i][0], int(A[i][1])] for i in range(N)]
a = deque(A)
time = 0
while len(a) > 0:
	temp = a.popleft()
	if temp[1] - q <= 0:
		time += temp[1]
		print(temp[0], time)
	else:
		time += q
		temp[1] -= q
		a.append(temp)


queue = []
(n, q) = map(int, input().split())
for i in range(n):
	(name, time) = input().split()
	queue.append([name, int(time)])
t = 0
while len(queue) > 0:
	entry = queue.pop(0)
	t += min(entry[1], q)
	entry[1] -= q
	if entry[1] > 0:
		queue.append(entry)
	else:
		print('{} {}'.format(entry[0], t))


from collections import deque
(n, q) = map(int, input().split())
que = deque([])
for _ in range(n):
	que.append((int(x) if x.isdigit() else x for x in input().split()))
log = 0
while que:
	(name, lefttime) = que.popleft()
	if lefttime <= q:
		log += lefttime
		print(name, log)
	else:
		lefttime -= q
		log += q
		que.append((name, lefttime))


from collections import deque
(n, q) = [int(i) for i in input().split(' ')]
ps = deque()
for i in range(n):
	p = [i for i in input().split(' ')]
	ps.append([p[0], int(p[1])])
time = 0
while len(ps) > 0:
	p = ps.popleft()
	tmp = p[1] - q
	if tmp > 0:
		ps.append([p[0], tmp])
		time += q
	else:
		time += p[1]
		print(p[0], str(time))


from collections import deque
(n, q) = map(int, input().split())
queue = deque([])
for _ in range(n):
	(n, t) = input().split()
	t = int(t)
	queue.append([n, t])
now = 0
while queue:
	if queue[0][1] <= q:
		now += queue[0][1]
		name = queue[0][0]
		queue.popleft()
		print(name, now)
	else:
		now += q
		(n, t) = queue.popleft()
		t -= q
		queue.append([n, t])


def main():
	(n, q) = map(int, input().split())
	que = []
	for _ in range(n):
		(name, time) = input().split()
		que += [[name, int(time)]]
	anst = 0
	while que != []:
		temp = que.pop(0)
		if temp[1] <= q:
			anst += temp[1]
			print(f'{temp[0]} {anst}')
		else:
			anst += q
			que += [[temp[0], temp[1] - q]]
main()


from collections import deque
(n, q) = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque((f(*input().split()) for _ in range(n)))
cnt = 0
ans = []
while dq:
	(s, t) = dq.popleft()
	if t <= q:
		cnt += t
		ans.append(f'{s} {cnt}')
	else:
		cnt += q
		dq.append((s, t - q))
print(*ans, sep='\n')


(n, q) = map(int, input().split())
pro = []
for _ in range(n):
	x = input().split()
	pro.append([x[0], int(x[1])])
curti = 0
while len(pro) > 0:
	curpro = pro.pop(0)
	if curpro[1] > q:
		curpro[1] -= q
		curti += q
		pro.append(curpro)
	else:
		curti += curpro[1]
		print(curpro[0], curti)


from collections import deque
(n, q) = (int(s) for s in input().split())
que = deque()
for i in range(n):
	(name, ts) = input().split()
	que.append((name, int(ts)))
t = 0
while que:
	(name, time) = que.popleft()
	if time <= q:
		t += time
		print(name, t)
	else:
		t += q
		que.append((name, time - q))


(n, q) = map(int, input().split())
process = [input().split() for _ in range(n)]
time = 0
while process:
	p = process.pop(0)
	if int(p[1]) <= q:
		time += int(p[1])
		print(p[0], time)
	else:
		time += q
		process.append([p[0], int(p[1]) - q])


from collections import deque
(n, q) = map(int, input().split())
que = deque()
for _ in range(n):
	(s, t) = input().split()
	t = int(t)
	que.append((s, t))
t = 0
while len(que) > 0:
	(name, time) = que.popleft()
	if time <= q:
		t += time
		print('{} {}'.format(name, t))
	else:
		que.append((name, time - q))
		t += q


(n, q) = map(int, input().split())
ps = []
total = 0
for i in range(n):
	(name, time) = map(str, input().split())
	ps.append([name, int(time)])
while len(ps) > 0:
	if ps[0][1] <= q:
		total += ps[0][1]
		print(ps[0][0], total)
		del ps[0]
	else:
		total += q
		ps[0][1] -= q
		ps.append(ps[0])
		del ps[0]


(n, q) = map(int, input().split())
tasks = [(li[0], int(li[1])) for li in [input().split() for _ in range(n)]]
elapsed = 0
while len(tasks):
	(name, time) = tasks.pop(0)
	if time > q:
		elapsed += q
		tasks.append((name, time - q))
	else:
		elapsed += time
		print('{0} {1}'.format(name, elapsed))


from collections import deque
(n, q) = map(int, input().split())
queue = deque([])
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	queue.append([name, time])
t = 0
while len(queue) > 0:
	top = queue.popleft()
	spend = min(top[1], q)
	top[1] -= spend
	t += spend
	if top[1] == 0:
		print(top[0], t)
	else:
		queue.append(top)


from collections import deque
(n, q) = map(int, input().split())
queue = deque([])
for i in range(n):
	(a, b) = input().split()
	queue.append([a, int(b)])
clock = 0
while len(queue) > 0:
	now = queue.popleft()
	if now[1] > q:
		queue.append([now[0], now[1] - q])
		clock += q
	else:
		print(now[0], clock + now[1])
		clock += now[1]


(n, p) = [int(i) for i in input().split()]
queue = []
for i in range(n):
	(name, time) = input().split()
	queue.append((name, int(time)))
moment = 0
while queue:
	(name, time) = queue.pop(0)
	remain = time - p
	if remain <= 0:
		moment += time
		print(name, moment)
	else:
		moment += p
		queue.append((name, remain))


[n, q] = [int(x) for x in input().split()]
qs = []
for _ in range(n):
	al = input().split()
	qs.append([al[0], int(al[1])])
t = 0
while len(qs) > 0:
	task = qs.pop(0)
	if task[1] <= q:
		t += task[1]
		print(task[0], t)
	else:
		t += q
		task[1] -= q
		qs.append(task)


from collections import deque
robin = deque()
(n, q) = map(int, input().split())
for i in range(n):
	(p, t) = input().split()
	robin.append([p, int(t)])
time = 0
while len(robin) != 0:
	proc = robin.popleft()
	if proc[1] <= q:
		print(proc[0], time + proc[1])
		time += proc[1]
	else:
		proc[1] -= q
		robin.append(proc)
		time += q


from collections import deque
(n, q) = map(int, input().split())
que = deque([])
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
t = 0
while len(que) > 0:
	atop = que.popleft()
	spend = min(atop[1], q)
	t += spend
	atop[1] -= spend
	if atop[1] == 0:
		print(atop[0], t)
	else:
		que.append(atop)


(n, q) = list(map(int, input().split()))
ls = []
for i in range(n):
	(a, b) = input().split()
	ls.append([a, int(b)])
done = []
t = 0
while ls != []:
	task = ls.pop(0)
	if task[1] <= q:
		t += task[1]
		done.append([task[0], t])
	else:
		task[1] -= q
		t += q
		ls += [task]
for i in range(n):
	print(*done[i])


from collections import deque
(n, q) = map(int, input().split())
que = deque([])
tmcnt = 0
for i in range(n):
	(name, timer) = input().split()
	que.append([name, int(timer)])
while len(que) != 0:
	now = que.popleft()
	nt = now[1] - q
	if nt <= 0:
		tmcnt += now[1]
		print(now[0], tmcnt)
	else:
		now[1] -= q
		tmcnt += q
		que.append(now)


(n, q) = (int(x) for x in input().split())
process = [input().split() for _ in range(n)]
time = 0
while process:
	p = process.pop(0)
	if int(p[1]) <= q:
		time += int(p[1])
		print(p[0], time)
	else:
		time += q
		process.append([p[0], int(p[1]) - q])


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(k, v) = input().split()
	queue.append([k, int(v)])
time = 0
while len(queue) > 0:
	if queue[0][1] > q:
		time += q
		queue[0][1] -= q
		queue.append(queue.pop(0))
	else:
		time += queue[0][1]
		print(queue.pop(0)[0], time)


(n, q) = [int(i) for i in input().split()]
nt = [[c for c in input().split()] for i in range(n)]
for i in range(n):
	nt[i][1] = int(nt[i][1])
t = 0
while len(nt) > 0:
	p = nt.pop(0)
	if p[1] <= q:
		t += p[1]
		print(p[0], t)
	else:
		t += q
		p[1] -= q
		nt.append(p)


(n, q) = map(int, input().split())
queue = list()
for i in range(n):
	p = input().split()
	queue.append({'name': p[0], 'time': int(p[1])})
t = 0
while len(queue) > 0:
	proc = queue.pop(0)
	if q < proc['time']:
		proc['time'] -= q
		t += q
		queue.append(proc)
	else:
		t += proc['time']
		print(proc['name'], t)


from collections import deque
(n, q) = map(int, input().split())
Q = deque()
ans = []
total = 0
for _ in range(n):
	(p, t) = input().split()
	Q.append([p, int(t)])
while len(Q) > 0:
	(p, t) = Q.popleft()
	if t <= q:
		total += t
		ans.append([p, total])
	else:
		total += q
		Q.append([p, t - q])
for v in ans:
	print(v[0], v[1])


(n, q) = [int(x) for x in input().split()]
Q = []
for i in range(n):
	Q.append(input().split())
t = 0
res = []
while Q != []:
	if int(Q[0][1]) <= q:
		res.append([Q[0][0], int(Q[0][1]) + t])
		t += int(Q[0][1])
	else:
		Q.append([Q[0][0], int(Q[0][1]) - q])
		t += q
	del Q[0]
for i in res:
	print(i[0] + ' ' + str(i[1]))


(n, q) = list(map(int, input().split()))
works = []
for i in range(n):
	works.append(list(input().split()))
count = 0
while len(works) != 0:
	work = works.pop(0)
	time = int(work[1])
	if time > q:
		count += q
		work[1] = time - q
		works.append(work)
	else:
		count += time
		print(work[0], count)


(n, p) = map(int, input().split())
ps = []
for i in range(n):
	ps.append(input().split())
	ps[i][1] = int(ps[i][1])
t = 0
while len(ps) > 0:
	if ps[0][1] > p:
		ps.append([ps[0][0], ps[0][1] - p])
		t += p
		ps.pop(0)
	else:
		t += ps[0][1]
		print('{0} {1}'.format(ps[0][0], t))
		ps.pop(0)


(n, q) = map(int, input().split())
a = [list(map(str, input().split())) for _ in range(n)]
ans = []
time = 0
while a:
	j = a.pop(0)
	if int(j[1]) > q:
		j[1] = str(int(j[1]) - q)
		a.append(j)
		time += q
	else:
		time += int(j[1])
		ans.append([j[0], str(time)])
for i in range(n):
	print(' '.join(ans[i]))


(n, q) = map(int, input().split())
l = []
for i in range(n):
	t = []
	for j in input().split():
		t += [j]
	t[1] = int(t[1])
	l += [t]
c = 0
while len(l) > 0:
	if l[0][1] > q:
		l[0][1] -= q
		l += [l[0]]
		l.pop(0)
		c += q
	else:
		c += l[0][1]
		print(l[0][0], c)
		l.pop(0)


(n, q) = map(int, input().split())
queue = []
for i in range(n):
	(name, time) = input().split()
	queue.append((name, int(time)))
t = 0
while queue:
	(name, time) = queue.pop(0)
	t += min(q, time)
	if time > q:
		queue.append((name, time - q))
	else:
		print(name, t)


(n, q) = map(int, input().split())
proc = []
for _ in range(n):
	(name, time) = input().split()
	proc.append([name, int(time)])
time = 0
while proc:
	p = proc.pop(0)
	if p[1] - q <= 0:
		time += p[1]
		print(p[0], time)
	else:
		p[1] -= q
		time += q
		proc.append(p)


(n, q) = list(map(int, input().split()))
que = []
for _ in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
total_time = 0
while len(que) != 0:
	p = que.pop(0)
	if p[1] - q <= 0:
		total_time += p[1]
		print(p[0], total_time)
	else:
		total_time += q
		p[1] -= q
		que.append(p)


(n, q) = map(int, input().split())
l = []
for i in range(n):
	v = input().split()
	l.append([v[0], int(v[1])])
ans = []
t = 0
while len(l) > 0:
	v = l.pop(0)
	v[1] -= q
	if v[1] <= 0:
		t += q + v[1]
		ans.append([v[0], str(t)])
	else:
		t += q
		l.append(v)
for v in ans:
	print(' '.join(v))


from collections import deque
(n, p) = map(int, input().split())
cnt = 0
r = deque((input().split() for _ in range(n)))
while r:
	(name, val) = r.popleft()
	val_af = max(int(val) - p, 0)
	if val_af != 0:
		r.append([name, val_af])
		cnt += p
	else:
		cnt += int(val)
		print(name, cnt)


from collections import deque
(n, q) = map(int, input().split())
process = deque()
now = 0
for _ in range(n):
	(name, time) = input().split()
	process.append([name, int(time)])
while process:
	task = process.popleft()
	now += min(q, task[1])
	if q < task[1]:
		process.append([task[0], task[1] - q])
	else:
		print(task[0], now)


(n, q) = map(int, input().split())
P = {}
Q = []
for i in range(n):
	(a, b) = input().split()
	P[a] = int(b)
	Q.append(a)
time = 0
while len(Q) > 0:
	if P[Q[0]] > q:
		time += q
		P[Q[0]] -= q
		Q.append(Q[0])
	else:
		time += P[Q[0]]
		P[Q[0]] = 0
		print('{} {}'.format(Q[0], time))
	Q.remove(Q[0])


(n, f) = map(int, input().split())
d = []
for _ in range(n):
	i = input().split()
	d.append([i[0], int(i[1])])
t = 0
while len(d):
	if d[0][1] > f:
		t += f
		d[0][1] -= f
		d.append(d.pop(0))
	else:
		t += d[0][1]
		a = d.pop(0)
		print(a[0], t)


from collections import deque
(n, q) = map(int, input().split())
Q = deque()
for i in range(n):
	(name, time) = input().split()
	Q.append([name, int(time)])
sum = 0
while Q:
	qt = Q.popleft()
	if qt[1] <= q:
		sum += qt[1]
		print(qt[0], sum)
	else:
		sum += q
		qt[1] -= q
		Q.append(qt)


import collections
import sys
(n, q) = map(int, input().split())
data = [[i for i in input().split()] for i in range(n)]
time = 0
while data:
	task = data[0]
	del data[0]
	if int(task[1]) <= q:
		time += int(task[1])
		print(task[0], time)
	else:
		time += q
		task[1] = str(int(task[1]) - q)
		data.append(task)


import sys
from collections import deque

def m():
	s = sys.stdin.readlines()
	q = int(s[0].split()[1])
	f = lambda x, y: (x, int(y))
	d = deque((f(*e.split()) for e in s[1:]))
	(t, a) = (0, [])
	while d:
		(k, v) = d.popleft()
		if v > q:
			v -= q
			t += q
			d.append([k, v])
		else:
			t += v
			a += [f'{k} {t}']
	print('\n'.join(a))
if '__main__' == __name__:
	m()


(n, q) = map(int, input().split())
p = []
time = 0
for i in range(n):
	p.append(input().split())
	p[i][1] = int(p[i][1])
while len(p) > 0:
	if p[0][1] <= q:
		time += p[0][1]
		print('{0} {1}'.format(p[0][0], time))
		p.pop(0)
	else:
		time += q
		p[0][1] -= q
		p.append(p[0])
		p.pop(0)


(n, q) = list(map(int, input().split()))
name = []
time = []
sum = 0
for i in range(n):
	(na, ti) = input().split()
	name.append(na)
	time.append(int(ti))
while len(name) > 0:
	na = name.pop(0)
	ti = time.pop(0)
	if ti > q:
		sum += q
		name.append(na)
		time.append(ti - q)
	else:
		sum += ti
		print(na, sum)


from collections import deque
(n, p) = map(int, input().split())
que = deque([])
gTime = 0
for i in range(n):
	(name, time) = input().split()
	time = int(time)
	que.append([name, time])
t = 0
while len(que) > 0:
	top = que.popleft()
	x = min(top[1], p)
	top[1] -= x
	t += x
	if top[1] == 0:
		print(top[0], t)
	else:
		que.append(top)


(n, q) = map(int, input().split())
process = [input().split() for _ in range(n)]
task = process[:]
cnt = 0
last = 0
while task:
	t = task.pop(0)
	(name, time) = (t[0], int(t[1]))
	if time <= q:
		cnt += time
		print(name, cnt)
	else:
		task.append([name, str(time - q)])
		cnt += q


from collections import deque
(n, q) = map(int, input().split())
d = deque([input().split() for _ in range(n)])
a = []
tt = 0
while d:
	t = int(d[0][1])
	if t > q:
		tt += q
		d.append([d[0][0], str(t - q)])
		d.popleft()
	else:
		tt += t
		p = d.popleft()
		a.append([p[0], tt])
for (n, t) in a:
	print(n, t)


from collections import deque
(n, q) = map(int, input().split())
n_t = deque()
for i in range(n):
	(s, j) = input().split()
	n_t.append([str(s), int(j)])
timer = 0
while n_t:
	(s, i) = n_t.popleft()
	if i > q:
		i -= q
		timer += q
		n_t.append([s, i])
	else:
		timer += i
		print('{0} {1}'.format(s, timer))


from collections import deque
(N, Q) = [int(i) for i in input().split()]
q = deque()
for _ in range(N):
	p = input().split()
	q.append((p[0], int(p[1])))
current_time = 0
while len(q) > 0:
	p = q.popleft()
	if p[1] > Q:
		current_time += Q
		q.append((p[0], int(p[1]) - Q))
	else:
		current_time += p[1]
		print(p[0], current_time)


from collections import deque
(n, q) = map(int, input().split())
d = deque()
for i in range(n):
	tmp = input().split()
	tmp[1] = int(tmp[1])
	d.append(tmp)
time = 0
while len(d) > 0:
	tmp = d.popleft()
	time += min(q, tmp[1])
	if tmp[1] > q:
		tmp[1] -= q
		d.append(tmp)
	else:
		print(tmp[0] + ' ' + str(time))


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]


from collections import deque as D
(n, q) = map(int, input().split())
dq = D()
for i in range(n):
	(p, t) = map(str, input().split())
	dq.append([p, int(t)])
total = 0
while dq:
	pt = dq.popleft()
	if pt[1] <= q:
		total += pt[1]
		print(pt[0], total)
	else:
		total += q
		dq.append([pt[0], pt[1] - q])


(n, q) = [int(x) for x in input().split()]
pro = [[0, 0]] * n
for i in range(n):
	pro[i] = [str(x) for x in input().split()]
cnt = 0
while bool(pro):
	a = pro.pop(0)
	if int(a[1]) - q <= 0:
		cnt += int(a[1])
		print('{} {}'.format(a[0], cnt))
	else:
		cnt += q
		a[1] = str(int(a[1]) - q)
		pro.append(a)


(n, q) = map(int, input().split())
p = [0 for _ in range(n)]
time = 0
for i in range(n):
	(a, m) = input().split()
	p[i] = [a, int(m)]
while len(p) > 0:
	t = p.pop(0)
	if t[1] <= q:
		print(t[0], time + t[1])
		time += t[1]
	else:
		time += q
		t[1] -= q
		p.append(t)


from collections import deque as dq
(n, q) = map(int, input().split())
queue = dq([])
time = 0
for i in range(n):
	(a, b) = input().split()
	queue.append([a, int(b)])
while queue:
	p = queue.popleft()
	if p[1] > q:
		p[1] -= q
		time += q
		queue.append(p)
	else:
		time += p[1]
		print(p[0], time)


import sys
from collections import deque
readline = sys.stdin.readline
(n, q) = map(int, input().split())
f = lambda a, b: (a, int(b))
dq = deque((f(*readline().split()) for _ in range(n)))
cnt = 0
ans = []
while dq:
	(s, t) = dq.popleft()
	if t <= q:
		cnt += t
		ans.append(f'{s} {cnt}')
	else:
		cnt += q
		dq.append((s, t - q))
print(*ans, sep='\n')


from collections import deque
(n, q) = list(map(int, input().split()))
P = deque([[p, int(t)] for (p, t) in [input().split() for _ in range(n)]])
time = 0
while len(P) > 0:
	pi = P.popleft()
	if pi[1] > q:
		P.append([pi[0], pi[1] - q])
		time += q
	else:
		time += pi[1]
		print(pi[0], time)


from collections import deque
(n, q) = map(int, input().split())
nt = [list(input().split()) for _ in range(n)]
que = deque(nt)
time = 0
while len(que) > 0:
	a = que.popleft()
	if int(a[1]) > q:
		que.append([a[0], int(a[1]) - q])
		time += q
	else:
		time += int(a[1])
		print(a[0], time)


import sys
import collections
s = sys.stdin.readlines()
(n, q) = map(int, s[0].split())
d = collections.deque((e.split() for e in s[1:]))
t = 0
while d:
	(k, v) = d.popleft()
	v = int(v)
	if v > q:
		v -= q
		t += q
		d.append([k, v])
	else:
		t += v
		print(k, t)


(n, q) = map(int, input().split())
array = [[s for s in input().split()] for i in range(n)]
total_time = 0
while len(array) != 0:
	a = array.pop(0)
	a_t = int(a[1])
	if a_t - q <= 0:
		total_time += a_t
		a[1] = total_time
		print(a[0], a[1])
	else:
		a[1] = a_t - q
		array.append(a)
		total_time += q


from collections import deque
(N, q) = map(int, input().split())
A = []
for i in range(N):
	(s, a) = input().split()
	A.append((s, int(a)))
queue = deque(A)
t = 0
while queue:
	(s, a) = queue.popleft()
	if a - q > 0:
		t += q
		queue.append((s, a - q))
	else:
		t += a
		print(s, t)


a = input().split()
a = [int(i) for i in a]
b = []
cnt = 0
time = 0
for i in range(a[0]):
	b.append(input().split())
	cnt += int(b[i][1])
i = 0
while cnt != time:
	if int(b[i][1]) <= a[1]:
		time += int(b[i][1])
		print(b[i][0], time)
		b.pop(0)
	else:
		b[i][1] = int(b[i][1]) - a[1]
		time += a[1]
		b.append(b.pop(i))


import collections as cols
(n, q) = [int(x) for x in input().split()]
qu = cols.deque()
for i in range(n):
	tmp = [x for x in input().split()]
	qu.append([tmp[0], int(tmp[1])])
num = n
t = 0
while num > 0:
	tmp = qu.popleft()
	if tmp[1] > q:
		t += q
		qu.append([tmp[0], tmp[1] - q])
	else:
		t += tmp[1]
		num -= 1
		print(tmp[0], t)


from collections import deque
(n, q) = map(int, input().split())
a = deque()
for i in range(n):
	(name, time) = input().split()
	a.append([name, int(time)])
cnt = 0
while a:
	t = a.popleft()
	if t[1] <= q:
		cnt += t[1]
		print(t[0], cnt)
	else:
		cnt += q
		t[1] -= q
		a.append(t)


(n, q) = map(int, input().split())
A = []
B = []
C = []
t = 0
for i in range(n):
	(x, y) = input().split()
	A.append([x, int(y)])
while len(A) > 0:
	f = A[0][1]
	if f <= q:
		t += f
		B.append(A[0][0])
		C.append(t)
	else:
		t += q
		A[0][1] -= q
		A.append(A[0])
	A.pop(0)
for j in range(len(B)):
	print(B[j], C[j])