Problem:
There are n processes in a queue. Each process has name i and time i .  The round-robin scheduling handles the processes in order. A round-robin scheduler gives each process a quantum (a time slot) and interrupts the process if it is not completed by then. The process is resumed and moved to the end of the queue, then the scheduler handles the next process in the queue. For example, we have the following queue with the quantum of 100ms. A(150) - B(80) - C(200) - D(200) First, process A is handled for 100ms, then the process is moved to the end of the queue with the remaining time (50ms). B(80) - C(200) - D(200) - A(50) Next, process B is handled for 80ms. The process is completed with the time stamp of 180ms and removed from the queue. C(200) - D(200) - A(50) Your task is to write a program which simulates the round-robin scheduling .  Input  n  q  name 1 time 1  name 2 time 2 ...  name n time n  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. name i and time i are separated by a single space. Output For each process, prints its name and the time the process finished in order. Constraints  1 ≤ n ≤ 100000  1 ≤ q ≤ 1000  1 ≤ time i ≤ 50000  1 ≤ length of name i ≤ 10  1 ≤ Sum of time i ≤ 1000000  Sample Input 1 5 100
p1 150
p2 80
p3 200
p4 350
p5 20 Sample Output 1 p2 180
p5 400
p1 450
p3 550
p4 800 Notes Template in C

Solution (Python):
import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n, q = LI()
    nt = collections.deque()

    for _ in range(n):
        name, time = LSS()
        nt.append([name, int(time)])

    endtime = 0
    while nt:
        name, time = nt.popleft()
        if time>q:
            nt.append([name, time-q])
            endtime += q
        else:
            endtime+=time
            print(name, endtime)

if __name__ == '__main__':
    resolve()

Solution (Python):
class queue:
    def __init__(self):
        self.V = []
        self.T = []
    def isEmpty(self):
        return len(self.V) == 0
    def enqueue(self,lis):
        self.T.append(lis[0])
        self.V.append(int(lis[1]))
    def dequeue(self):
        title = self.T[0]
        value = self.V[0]
        self.T = self.T[1:len(self.T)]
        self.V = self.V[1:len(self.V)]
        return [title,value]
n,q = list(map(int,input().split()))
time = 0
Q = queue()
totalT = 0
for i in range(n):
    arg = input().split()
    totalT += int(arg[1])
    if int(arg[1]) <= q:
        time += int(arg[1])
        print(f"{arg[0]} {time}")
    else:
        time += q
        Q.enqueue([arg[0],int(arg[1])-q])
    
while time<totalT:
    l = Q.dequeue()
    if l[1]<=q:
        time += l[1]
        print(f"{l[0]} {time}")
    else:
        time += q
        Q.enqueue([l[0],int(l[1])-q])

Solution (Python):
from collections import deque

n,q = map(int,input().split())
Q = deque()

for _ in range(n):
    p = [x for x in input().split()]
    Q.append(p)

account = 0
i = 0
while len(Q)!=0:
    if int(Q[i][1]) > q:
        Q[i][1] = int(Q[i][1])-q
        Q.append(Q[i])
        Q.popleft()
        account += q
    else:
        account += int(Q[i][1])
        print(Q[0][0],account)
        Q.popleft()

Solution (Python):
from collections import deque

a,b = map(int, input().split())
d = deque()
s = deque()
for i in range(a):
    c,g = map(str,input().split())
    d.append(int(g))
    s.append(c)

ans_s = []
ans_num = []
count = 0
while True:
    count += b
    if len(d) == 0:
        break
    e = d.popleft()
    f = s.popleft()
    if e - b <= 0:
        ans_s.append(f)
        count += (e-b)
        ans_num.append(count)
    else:
        d.append(e - b)
        s.append(f)
for i in range(a):
    print(str(ans_s[i]) + ' ' + str(ans_num[i]))

Solution (Python):
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]])

Solution (Python):
import collections
n,q = map(int,input().split())
item = []
for i in range(n):
    p,val = input().split()
    item.append([p,int(val)])
Queue = collections.deque([])
for i in range(n):
    Queue.append([item[i][0],item[i][1]])
time = 0
while Queue:
    name,val = Queue.popleft()
    if val>q:
        val -= q
        Queue.append([name,val])
        time += q
    else:
        time += val
        print(name,time)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
def round_robin(queue_list, t):
    elapsed_time = 0
    while len(queue_list) > 0:
        p, c = queue_list.pop(0)
        if c-t > 0:
            elapsed_time += t
            queue_list.append([p, c-t])
        else:
            elapsed_time += c
            print("{} {}".format(p,elapsed_time))

N, cpu_time = map(int,input().split())
queue_list = []
for i in range(N):
    process,cost = input().split()
    queue_list.append([process, int(cost)])

round_robin(queue_list, cpu_time)

Solution (Python):
from collections import deque

n, q = map(int, input().split())
deq = deque()

for i in range(n):
    name, time = input().split(); time = int(time)
    deq.append((time, name))

ans = []
cur = 0

while deq:
    time, name = deq.popleft()
    if time <= q:
        cur += time
        ans.append('%s %d' % (name, cur))
    else:
        cur += q
        deq.append((time - q, name))

print(*ans, sep='\n')

Solution (Python):
class queue:
    val = []
    def __init__(self,arr):
        self.val = arr

    def push_queue(self,el):
        self.val.append(el)

    def pop_queue(self):
        return self.val.pop(0)



n,q = map(int,input().split())
process = [input().split() for a in range(n)]
for a in process:
    a[1] = int(a[1])

queued_process = queue(process)

time_count = 0
while True:
    if queued_process.val[0][1] > q:
        time_count += q
        queued_process.val[0][1] -= q
        queued_process.push_queue(queued_process.pop_queue())
    else:
        time_count += queued_process.val[0][1]
        queued_process.val[0][1] = 0
        print(f'{queued_process.val[0][0]} {time_count}')
        queued_process.pop_queue()
    if queued_process.val == []:
        break

Solution (Python):
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)

Solution (Python):
import math
import sys
import itertools


def main():
  n,q = map(int, input().split())
  process = [input().split() for _ in range(n)]
  

  total_time = 0

  while len(process) > 0:
    time = int(process[0][1])

    if time <= q:
      total_time += time
      print(process[0][0], total_time)
    else:
      total_time += q
      process.append([process[0][0], str(time - q)])

    process.pop(0)

if __name__ == '__main__':
  main()

Solution (Python):
nq = list(map(int, input().split()))
queue = list()

for i in range(nq[0]):
    queue.append(input().split())

ti = 0
cur = 0
while int(queue[cur][1]) > 0:
    num = queue[cur]
    syori = int(num[1]) - nq[1]
    
    if syori > 0:
        num[1] = syori
        queue.append(num)
        ti += nq[1]
    else:
        ti += int(num[1])
        print('{} {}'.format(num[0], ti))
    cur += 1
    
    if len(queue) <= cur:
        break

Solution (Python):
num,MAX = map(int,input().split())
queue = []
total_time = 0
[queue.append(input().split()) for _ in range(num)]
flag = 1
while flag:
  if len(queue) == 0:
    break
  queue[0][1] = int(queue[0][1])
  total_time += min(MAX,queue[0][1])
  queue[0][1] -= min(MAX,queue[0][1])
  temp = queue[0]
  queue.pop(0)
  if temp[1] == 0:
    print(f"{temp[0]} {total_time}")
    continue
  queue.append(temp)

Solution (Python):
from collections import deque

if __name__ == '__main__':
    n, q = list(map(int, input().split()))
    queue = deque()
    t = 0

    for _ in range(n):
        name, time = input().split()
        queue.append((name, int(time)))

    while len(queue):
        name, time = queue.popleft()

        if time <= q:
            t += time
            print(name, t)
        else:
            t += q
            time -= q
            queue.append((name, time))

Solution (Python):
n,v=map(int,input().split())
p_array=[]
total_num=0
i_cnt=0
for i in range(n):
    p_array.append(list(input().split()))
    p_array[i][1]=int(p_array[i][1])
while len(p_array) != 0:
    for i in range(n):
        if v < p_array[i-i_cnt][1]:
            p_array[i-i_cnt][1] -= v
            total_num += v
        else:
            total_num+=p_array[i-i_cnt][1]
            print(p_array[i-i_cnt][0],total_num)
            p_array.pop(i-i_cnt)
            i_cnt += 1
            n -= 1
    i_cnt=0

Solution (Python):
class queClass:

	def __init__(self, qtime, valuelist):
		self.qtime = qtime
		self.valuelist = valuelist
		self.stime = 0

	def run(self):
		time = self.qtime
		if time < self.valuelist[0][1]:  
			self.valuelist.append([self.valuelist[0][0], self.valuelist[0][1] - time])
			self.valuelist.remove(self.valuelist[0])
			self.stime = self.stime + self.qtime
		else:  
			self.stime = self.stime + self.valuelist[0][1]
			print('{0} {1}'.format(self.valuelist[0][0], self.stime))
			self.valuelist.remove(self.valuelist[0])
			if len(self.valuelist) == 0:
				exit(0)
		return


n, q = map(int, input().split())
inplist = []
for i in range(0, n):
	a, b = input().split()
	inplist.append([a, int(b)])

que = queClass(q, inplist)
while True:
	que.run()

Solution (Python):
from collections import deque
n, q = map(int, input().split())
deq = deque()
for i in range(n):
    name, time = input().split(); time = int(time)
    deq.append((time, name))
ans = []
cur = 0
while deq:
    time, name = deq.popleft()
    if time <= q:
        cur += time
        ans.append("%s %d" % (name, cur))
    else:
        cur += q
        deq.append((time-q, name))
print(*ans, sep="\n")

Solution (Python):
class ArrayDeque:
    def __init__(self):
        self.a_size = 1
        self.a = [None]
        self.n = 0
        self.head = 0
        self.tail = 0

    def __bool__(self):
        return self.n != 0

    def _resize(self):
        new_a = [None] * ((self.n + 1) << 1)
        for i in range(self.n):
            new_a[i] = self.a[(self.head + i) & (self.a_size - 1)]
        self.a = new_a
        self.a_size = (self.n + 1) << 1
        self.head = 0
        self.tail = self.n

    def append(self, val):
        if self.n == self.a_size - 1:
            self._resize()
        self.a[self.tail] = val
        self.tail = (self.tail + 1) & (self.a_size - 1)
        self.n += 1

    def appendleft(self, val):
        if self.n == self.a_size - 1: 
            self._resize()
        self.head = (self.head - 1) & (self.a_size - 1)
        self.a[self.head] = val
        self.n += 1

    def popleft(self):
        if self.n == 0:
            raise IndexError()
        val = self.a[self.head]
        self.head = (self.head + 1) & (self.a_size - 1)
        self.n -= 1
        if self.a_size >= 4 * self.n + 2:
            self._resize()
        return val

    def pop(self):
        if self.n == 0:
            raise IndexError()
        self.tail = (self.tail - 1) & (self.a_size - 1)
        val = self.a[self.tail]
        self.n -= 1
        if self.a_size >= 4 * self.n + 2:
            self._resize()
        return val


n, q = map(int, input().split())
info = [input().split() for i in range(n)]

que = ArrayDeque()
for name, time in info:
    que.append((name, int(time)))

ans_time = 0
ans = []
while que:
    name, time = que.popleft()
    if time > q:
        que.append((name, time - q))
        ans_time += q
    else:
        ans_time += time
        ans.append((name, ans_time))

for name, time in ans:
    print(name, time)

Solution (Python):
from collections import deque
a,b = map(int,input().split())
p = deque()
for i in range(a):
    v = input().split()
    p.append({v[0] : int(v[1])})

ans = 0
ans_list = []
while True:
    v = p.popleft()
    for k,i in v.items():
        if i - b <= 0:
            ans = ans + i
            l = {k:ans}
            ans_list.append(l)
        else:
            i = i -b
            v[k] = i
            p.append(v)
            ans = ans + b

    if len(p) == 0:
        break

for v in ans_list:
    for k,i in v.items():
        print('{} {}'.format(k,i))

Solution (Python):
import queue

n, q = map(int, input().split())
que = queue.Queue(n)

for i in range(n):
    name, time = input().split()
    que.put([name, int(time)])

c_time = 0
while True:
    try:
        name, time = que.get(block=False)
    except queue.Empty:
        break
    else:
        if time > q:
            que.put([name, time - q])
            c_time += q
        else:
            c_time += time
            print(name, c_time, sep=' ')

Solution (Python):
from typing import  Any


class FixedQueue:
    class Empty(Exception):
        pass

    class Full(Exception):
        pass

    def __init__(self, capacity: int) -> None:
        self.no = 0
        self.front = 0
        self.rear = 0
        self.capacity = capacity
        self.que = [None] * capacity

    def __len__(self) -> int:
        return  self.no

    def is_empty(self) -> bool:
        return self.no <= 0

    def is_full(self) -> bool:
        return  self.no >= self.capacity

    def enque(self, x: Any) -> None:
        if self.is_full():
            raise FixedQueue.Full
        self.que[self.rear] = x
        self.rear += 1
        self.no += 1
        if self.rear == self.capacity:
            self.rear = 0

    def deque(self) -> Any:
        if self.is_empty():
            raise FixedQueue.Empty
        x = self.que[self.front]
        self.front += 1
        self.no -= 1
        if self.front == self.capacity:
            self.front = 0
        return  x

current_time = 0
n, p = map(int, input().split())
Q = FixedQueue(n + 1)

for i in range(n):
    name, time = input().split()
    time = int(time)
    Q.enque([name, time])

while len(Q) > 0:
    u = Q.deque()
    c = min(p, u[1])
    u[1] -= c
    current_time += c
    if u[1] > 0:
        Q.enque(u)
    else:
        print(str(u[0]) + ' ' + str(current_time))

Solution (Python):
def readinput():
    n,q = list(map(int,input().split()))
    procs = []
    for i in range(n):
        name, strtime = input().split()
        inttime = int(strtime)
        procs.append([name, inttime])
    return n,q,procs

def dispach(t, q, procs):
    t0 = t
    

    proc = procs.pop(0)
    procname = proc[0]
    tremain = proc[1]
    if (tremain > q):
        proc[1] = tremain - q
        procs.append(proc)
        t0 += q
    else:
        t0 += tremain
        print(procname+' '+str(t0))

    
    return t0

def main(n,q,procs):
    t = 0
    
    while(len(procs)>0):
        t = dispach(t, q, procs)
        
    return

if __name__=='__main__':
    n,q,procs=readinput()
    
    main(n,q,procs)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import collections


def main():
    n, q = map(int, input().split())

    dq = collections.deque()
    for i in range(n):
        s = input().split()
        name = s[0]
        time = int(s[1])
        dq.append((name, time))

    cur_time = 0
    while len(dq) > 0:
        top = dq.popleft()
        
        if top[1] > q:
            dq.append((top[0], top[1] - q))
            cur_time += q
        else:
            print(top[0] + " " + str(cur_time+top[1]))
            cur_time += top[1]


main()

Solution (Python):
from collections import deque
n, q = map(int, input().split())

que = deque()
for _ in range(n):
    k, v = input().split()
    que.append([k, int(v)])


p_time = 0
while True:
    k, v = que.popleft() 
    if v <= q: 
        p_time += v
        print(k, p_time)
    else: 
        p_time += q
        que.append([k, v-q])

    if len(que) == 0: 
        break

Solution (Python):
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])

Solution (Python):



def roundRobin(task_list, q):
    total_time = 0
    task_result = []

    while len(task_list) > 0:
        task = task_list.pop(0)

        if task[1] > q:
            total_time += q
            task[1] -= q
            task_list.append(task)
        else:
            total_time += task[1]
            task_result.append([task[0], str(total_time)])

    for res in task_result:
        print(' '.join(res))


if __name__ == "__main__":
    n, q = [int(i) for i in input().split()]

    task_list = []

    for _ in range(n):
        key, value = input().split()
        task_list.append([key, int(value)])
    roundRobin(task_list, q)

Solution (Python):
from collections import deque

class task:
  
  def __init__(self,name,time):
    self.name = name
    self.time = time
    self.remain = time
  
  def getName(self):
    return self.name
  
  def getTime(self):
    return self.time
  
  def getRemain(self,wtime):
    self.remain -= wtime
    return self.remain

  

queue = deque()  

n, q = map(int,input().split())
for i in range(n):
  aN,aQ = input().split()
  aTask = task(aN,int(aQ))
  queue.append(aTask)

elapse = 0
remain = 0

while len(queue) > 0:
  aTask = queue.popleft()
  remain = aTask.getRemain(q)
  if remain <= 0 :
    elapse += remain + q
    print(aTask.getName() , elapse)
  else:
    elapse += q
    queue.append(aTask)

Solution (Python):
number=input().split()
n=int(number[0])
q=int(number[1])
answer=[]
time=0
head=0

Que=[input().split() for i in range(n)]
for i in range(n):
    Que[i][1]=int(Que[i][1])
    Que[i].append(0)
    
while Que!=[]:
    l=len(Que)
    if head==l:
        head=0
        
    else:
        if Que[head][1]<=q:
            time+=Que[head][1]
            Que[head][2]+=time
            tmp=Que.pop(head)
            answer.append(tmp)
            
        else:
            time+=q
            Que[head][1]-=q
            head+=1
            
for i in range(n):
    print(answer[i][0],answer[i][2])

Solution (Python):
n,q = input().split()
n = int(n)
q = int(q)

queue = []

class process:
    def __init__(self,a,b):
        self.name = a
        self.time = b


for i in range(n):
    a,b = input().split()
    b = int(b)
    queue.append(process(a,b))

def enqueue(proc):
    global queue
    queue.append(proc)

def dequeue():
    global queue
    return queue.pop(0)

def isEnpty():
    global queue
    return len(queue) != 0


def RoundRobinScheduling():
    now_time = 0
    while  isEnpty(): 
        now_process = dequeue()
        if now_process.time - q > 0:
            now_process.time = now_process.time - q
            enqueue(now_process)
            now_time = now_time + q
        else:
            now_time = now_time + now_process.time
            print(now_process.name,end=" ")
            print(now_time)


RoundRobinScheduling()

Solution (Python):
from sys import stdin
from collections import deque

n,g = map(int, stdin.readline().split())
processes = deque([(name,int(time)) for name,time in [stdin.readline().split() for _ in range(n)]])
total_time = 0
while len(processes) > 0:
    name,left_time = processes.popleft()
    if left_time > g:
        total_time += g
        left_time -= g
        processes.append((name,left_time))
    else:
        total_time += left_time
        print(name, total_time)

Solution (Python):
n, q = map(int,input().split())
queue = []
for i in range(n):
    proccess = list(input().split())
    queue.append([proccess[0],int(proccess[1])])
cnt = 0
time = 0

while cnt < n:
    if queue[0][1] <= q:
        time += queue[0][1]
        print(queue[0][0] + " " +str(time))
        cnt += 1
        del queue[0]
    elif queue[0][1] > q:
        time += q
        queue[0][1] -= q
        queue.append(queue.pop(0))

Solution (Python):
n,q = list(map(int,input().split()))

from collections import deque
que = deque([])

for i in range(n):
    que.append((int(x) if x.isdigit() else x for x in input().split()))

totaltime = 0

while que:
    name,lefttime = que.popleft()
    if lefttime <= q:
        totaltime += lefttime
        print(name,totaltime)
    else:
        lefttime -= q
        totaltime += q
        
        que.append((name,lefttime))

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
from collections import deque

n, q = map(int, input().split())
deq = []
total = 0
for _ in range(n):
    name, time = input().split()
    deq.append([name, int(time)])

Deq = deque(deq)
ans = []
time = 0
while Deq:
    process = Deq.popleft()
    tmp = process[1]
    remain = max(tmp-q, 0)
    if remain == 0:
        time += tmp
        ans.append([process[0], time])
    else:
        time += q
        Deq.append([process[0],remain])

for name, time in ans:
    print(name, time)

Solution (Python):
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

Solution (Python):
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)))
ans = []
time = 0
while len(que) > 0:
    x, y = que[0]
    que.popleft()
    if y > q:
        que.append((x, y - q))
        time += q
    else:
        time += y
        ans.append((x, time))
ans.sort(key = lambda x: x[1])
for i in ans:
    print(*i)

Solution (Python):
n,q=list(map(int,input().split()))
p=[]
for i in range(n):
    name_,time_=input().split()
    p.append([str(name_),int(time_)])

done_=[]
time_passed=0
while len(p)>0:
    box_=[]
    del_index=[]
    for i,process_ in enumerate(p):
        amount_=process_[1]-q
        if amount_>0:
            box_.append([process_[0],amount_])
            time_passed+=q
        else:
            time_passed+=min(q,process_[1])
            done_.append([process_[0],time_passed])   
            del_index.append(i)
    cnt=0
    for d in del_index:
        del p[d-cnt]
        cnt+=1
    p=box_
    
for k in range(n):
    print(*done_[k])

Solution (Python):
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])

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

for namae,num in ans:
    print(namae,num)

Solution (Python):
from collections import deque
n,q = map(int,input().split())
time = 0
tl=deque()
d=dict()
for i in range(n):
    key,value = input().split()
    d[key] = int(value)

for i in d.items():
    tl.append(i)


while len(tl)!=0:
    tmp = tl.popleft()
    if list(tmp)[1] <= q:
        time+=list(tmp)[1]
        
        
        
        print(f"{list(tmp)[0]} {time}")
    else:
        time+=q
        l=list(tmp)
        l[1]-=q
        nt=tuple(l)
        tl.append(nt)
        

"""

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()
"""

Solution (Python):
from collections import deque

n, q = map(int, input().split())
query = []

for i in range(n):
	n, t = map(str, input().split())
	query.append([n, int(t)])

d = deque(query)

ans = []

time = 0
while len(d) > 0:
	tmp = d.popleft()
	if tmp[1] > q:
		res = [tmp[0], tmp[1] - q]
		time += q
		d.append(res)
	else:
		time += tmp[1]
		ans.append([tmp[0], time])

for v in ans:
	print(v[0], v[1])

Solution (Python):
class Queue():

    def __init__(self, max_size):
        self.head = 0
        self.tail = 0
        self.max = max_size + 1
        self.q = [None for _ in range(self.max)]

    def is_empty(self):
        return self.head == self.tail

    def is_full(self):
        return self.head == (self.tail + 1) % self.max

    def enqueue(self, x):
        if self.is_full():
            print("Error: Overflow")
        self.q[self.tail] = x
        if self.tail + 1 == self.max:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            print("Error: Underflow")

        x = self.q[self.head]
        if self.head + 1 == self.max:
            self.head = 0
        else:
            self.head += 1

        return x


n, q = [int(x) for x in input().split()]
names = Queue(n)
time = Queue(n)

for _ in range(n):
    data = input().split()
    names.enqueue(data[0])
    time.enqueue(int(data[1]))

t = 0
while not names.is_empty():
    process = names.dequeue()
    process_time = time.dequeue()

    if process_time > q:
        names.enqueue(process)
        time.enqueue(process_time - q)
        t += q
    else:
        t += process_time
        print(process, t)

Solution (Python):
class Process():
    name = ""
    time = 0

    def __init__(self, name, time):
        self.name = name
        self.time = time


n, q = map(int, input().split(" "))

queue = []
for i in range(n):
    n, t = input().split(" ")
    queue.append(Process(n, int(t)))

qw = q
lap_time = 0
while len(queue) > 0:
    p = queue.pop(0)
    if qw != q and p.time - qw - q > 0:
        lap_time += qw + q
        p.time = p.time - qw - q
        qw = q
        queue.append(p)
    elif p.time - qw > 0:
        lap_time += qw
        p.time = p.time - qw
        qw = q
        queue.append(p)
    else:
        lap_time += p.time
        qw = q
        print(p.name, lap_time)

Solution (Python):
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

Solution (Python):


n, q = map(int, input().split())
l = [input().split() for _ in range(n)]
r = []
t = 0
while len(l) > 0:
    p = l.pop(0)
    pt = int(p[1])
    if pt >= q:
        t += q
        pt -= q
        if pt > 0:
            l.append([p[0], pt])
        else:
            r.append([p[0], t])
    else:
        t += pt
        r.append([p[0], t])
for x in r:
    print(' '.join(map(str, x)))

Solution (Python):
from collections import deque

n,qtime = list(map(int,input().split()))

Q = deque()

class Info:
    def __init__(self,arg_name,arg_rest_time):
        self.name = arg_name
        self.rest_time = arg_rest_time

for _ in range(n):
    tmp = input().split()
    Q.append(Info(tmp[0],int(tmp[1])))

current_time = 0

while len(Q)>0:
    info = Q.popleft()
    if info.rest_time > qtime:
        info.rest_time = info.rest_time - qtime
        current_time += qtime
        Q.append(info)
    else:
        current_time += info.rest_time
        print("%s %d"%(info.name,current_time))

Solution (Python):
import sys
from collections import deque

n,q = map(int,sys.stdin.readline().split())

dq = deque()

for x in range(n):
    x = sys.stdin.readline().split()
    dq.append([x[0],int(x[1])])
t = 0
while True:
    pr = dq.popleft()
    pr[1] -= q

    if pr[1] > 0:
        dq.append(pr)
        t += q
    else:
        t += pr[1] + q
        print(pr[0], t)

    if len(dq) == 0:
        break

Solution (Python):
from collections import deque

time = 0
finished = 0
n,q = map(int,input().split())


def rotate(A):
    global time,finished
    b = A.popleft()
    if b[1] - q > 0:
        b[1] -= q
        time += q
        A.append(b)
    else:
        time += b[1]
        finished += 1
        print(b[0],time)


def main():    
    order = deque([])
    for _ in range(n):
        tmp = list(input().split())
        tmp[1] = int(tmp[1])
        order.append(tmp)

    
    while finished < n:
        rotate(order)
    
if __name__ == "__main__":
    main()