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):
from collections import deque

N,Q = map(int,input().split())
deque_name = deque()
deque_time = deque()

for _ in range(N):
    a = list(input().split())
    deque_name.append(a[0])
    deque_time.append(int(a[1]))

now = 0
num=0
finished={}

while deque_name:

    name = deque_name.popleft()
    time = deque_time.popleft()

    if time<=Q:
        now+=time
        num+=1
        lst = [name,now]
        finished[num] = lst

    else:
        now+=Q
        time-=Q
        deque_name.append(name)
        deque_time.append(time)
    
for i in range(1,num+1):
    value = finished[i]
    print(' '.join(map(str,value)))

Solution (Python):

from collections import Counter, deque  












import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


def resolve():
    n, q = map(int, input().split())
    queue = deque([])  
    for i in range(n):
        name, time = input().split()
        time = int(time)
        queue.append([name, time])

    totaltime = 0  
    while queue:
        [name, time] = queue.popleft()
        if time <= q:  
            totaltime += time
            print(name, totaltime)
        else:
            time -= q
            queue.append([name, time])  
            totaltime += q


resolve()

Solution (Python):
from collections import deque

N,Q=map(int,input().split())
name_time=[[0]*2 for _ in range(N)]
for i in range(N):
    tmp=input().split()
    name_time[i][0]=tmp[0]
    name_time[i][1]=tmp[1]

q=deque()
q.extend(name_time)
now=0
while q:
    tmp=q.popleft()
    name=tmp[0]
    time=int(tmp[1])
    if time <= Q:
        now += time
        print(name,now)
    else:
        now += Q
        time -= Q
        q.append([name,time])

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

t = 0
seq = []
while len(A)>0:
    name, time = A.popleft()
    if time > q:
        time -= q
        t += q
        A.append([name, time])
    else:
        t += time
        seq.append([name, t])

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

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)
    t += spend
    atop[1] -= spend
    if atop[1] == 0:
        print(atop[0],t)
    else:
        que.append(atop)

Solution (Python):





A = []
n, q = map(int, input().split())
for i in range(n):
    name, time = input().split()
    A.append((name, int(time)))

totaltime = 0
while len(A) > 0:
    
    name, time = A[0]
    A.remove(A[0])
    if (time - q) <= 0:
        totaltime += time
        print(name, totaltime)
    else:
        totaltime += q
        A.append((name, time - q))

Solution (Python):
N ,q = map(int,input().split())
A = [input() for i in range(N)]

B = []
for raw in A:
    name,time = raw.split()
    B.append({
    "name":name,
    "time":int(time)
    })

total_time = 0
now = 0
while len(B)> 0:
    if B[now]["time"] > q:
        B[now]["time"] = B[now]["time"] - q
        total_time += q
        if now == len(B)-1:
            now = 0
        else:
            now += 1
    else:
        total_time += B[now]["time"]
        print("{} {}".format(B[now]["name"],total_time))
        del B[now]
        if now == len(B):
            now = 0

Solution (Python):
import queue
n, q = map(int, input().split())
processes = queue.Queue()
logs = []
time_count = 0

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

while processes.empty() == False:
  process = processes.get()
  if process[1] > q:
    time_count += q
    process[1] -= q
    processes.put(process)
  else:
    time_count += process[1]
    logs.append([process[0], time_count])

for i in range(n):
  print(" ".join(map(str, logs[i])))

Solution (Python):
queue = []
sum_prc_time = 0

n, max_time = map(int, input().split())

def main_prc(prc_dat):
  global queue
  global sum_prc_time

  prc_time = int(prc_dat[-1])
  diff = prc_time - max_time

  if diff <= 0:
    sum_prc_time += prc_time
    print(f"{prc_dat[0]} {sum_prc_time}")
  else:
    sum_prc_time += max_time
    queue.append([prc_dat[0], diff])

for i in range(n):
  main_prc(input().split())

while 0 < len(queue):
  main_prc(queue.pop(0))

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

Solution (Python):
class P:
    def __init__(self,name,time):
        self.name = name
        self.time = time
n,q = map(int,input().split())
LEN = 100004 
head = 0
tail = n
Q = [P("",0) for _ in range(LEN)]
for i in range(n):
    Q[i].name,ti = input().split()
    Q[i].time = int(ti)
def enqueue(Q,x,tail):
    Q[tail] = x       
    if tail+1 == LEN:
        tail = 0
    else:
        tail += 1
    return tail
def dequeue(Q,head):
    x = Q[head]
    if head+1 == LEN:
        head = 0
    else:
        head += 1
    return x,head
totime = 0
while head != tail:
    x,head = dequeue(Q,head)
    if x.time <= q:
        totime += x.time
        print(x.name + " " + str(totime))
    else:
        totime += q
        x.time -= q
        tail = enqueue(Q,x,tail)

Solution (Python):
from collections import deque

i,qtime = map(int,input().split())
ttime = 0
deque = deque()
dic= {}
for s in range(i):
    name,value = map(str,input().split())
    dic[name] = int(value)
    deque.append(name)
while len(deque) > 0:
    time = qtime
    while time > 0:
        if dic[deque[0]] > time:
            dic[deque[0]] = dic[deque[0]] - time
            deque.append(deque.popleft())
            ttime += time
            time = 0
        else:
            ttime += dic[deque[0]]
            time = 0
            dic[deque[0]] = 0
            print("{0} {1}".format(deque[0],ttime))
            deque.popleft()

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

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):
queue = []
sum_prc_time = 0

n, max_time = map(int, input().split())

def main_prc(prc_dat):
  global queue
  global sum_prc_time

  prc_time = int(prc_dat[-1])
  diff = prc_time - max_time

  if diff <= 0:
    sum_prc_time += prc_time
    print(f"{prc_dat[0]} {sum_prc_time}")
  else:
    sum_prc_time += max_time
    queue.append([prc_dat[0], diff])

for i in range(n):
  main_prc(input().split())

while 0 < len(queue):
  main_prc(queue.pop(0))

Solution (Python):
class queue():
    def __init__(self,size,q):
        self.time={}
        self.finishlist=[]
        self.S=[None]*size
        self.q=q
        self.under=0
    def print(self):
        print(self.under)

    def enqueue(self,x):
        self.under+=1
        self.S[self.under]=x
    def unqueue(self):
        self.under-=1
        vari=self.S[1]
        del self.S[1]
        self.S.append(None)
        return vari
    def isEmpty(self):
        return self.under!=0
    def calc(self):
        alltime=0
        while(self.isEmpty()):
            process=self.unqueue()
            da=int(process[1])
            if int(process[1])>self.q:
                
                
                
                
                alltime+=self.q
                da-=self.q
                (process[1])=str(da)
                self.enqueue(process)
            else:
                alltime+=int(process[1])
                self.time[process[0]] = alltime


                self.finishlist.append(process[0])
        return self.time,self.finishlist

size,q=[int(x) for x in input().split()]
que=queue(size+1,q)

[que.enqueue(input().split()) for x in range(size)]
time,finishhantei=que.calc()
for name in finishhantei:
    print(name,time[name])

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

name = ['' for i in range(n)]
time = [0 for i in range(n)]
for i in range(n):
    x,y = input().split()
    name[i] = x
    time[i] = int(y)

ttl = 0
while 1:
    if len(name) == 0:
        break
    if time[0] - q <= 0:
        ttl += q - (q - time[0])
        ptime = ttl
        print(name[0],ptime)
        del name[0]
        del time[0]
    else:
        ttl += q
        time[0] -= q
        name.append(name[0])
        time.append(time[0])
        del name[0]
        del time[0]

Solution (Python):

class Queue():
    def __init__(self, size):
        self.size = size
        self.Q = [None for i in range(size)]
        self.top = 0
        self.btm = 0
    
    def enqueue(self, x):
        self.Q[self.btm] = x;
        self.btm += 1;
        if self.btm >= self.size:
            self.btm = 0
    
    def dequeue(self):
        ret = self.Q[self.top]
        self.Q[self.top] = None
        self.top += 1
        if self.top >= self.size:
            self.top = 0
        return ret
    
    def __repr__(self):
        list(self.Q)
    
class Process():
    def __init__(self, name, time):
        self.name = name
        self.time = time
    
    def getname(self):
        return self.name
    
    def proceed(self, q):
        if q <= self.time:
            self.time -= q
            return q;
        elif q > self.time:
            ret = self.time
            self.time = 0
            return ret
    
    def isFinished(self):
        return self.time == 0
    
    def __repr__(self):
        return self.name + " " + str(self.time)

n, q = (int(i) for i in input().split())
Q = Queue(n)
for i in range(0,n):
    data = input().split()   
    name = data[0]
    time = int(data[1])
    Q.enqueue(Process(name, time))
timeelapsed = 0
while True:
    p = Q.dequeue()
    if p == None:
        break
    timeelapsed += p.proceed(q)
    if p.isFinished():
        print(p.getname(), timeelapsed)
    else:
        Q.enqueue(p)

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):
from collections import deque

def main():
    n,q = map(int,input().split())
    d = deque()
    for _ in range(n):
        p,t = input().split()
        d.append([p,int(t)])

    time = 0
    while len(d):
        p,t = d.popleft()
        if t<=q:
            time += t
            print (p,time)
        else:
            time+=q
            d.append([p,t-q])

if __name__ == '__main__':
    main()

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

Solution (Python):
import heapq
import queue
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


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


N,LIMIT = map(int,input().split())
Q = deque()


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 > LIMIT:
        info.rest_time -= LIMIT
        current_time += LIMIT
        Q.append(info)
    else:
        current_time += info.rest_time
        print("%s %d"%(info.name,current_time))

Solution (Python):

class Queue:
    def __init__(self, size=100005):
        self.size = size
        self.q = [None] * self.size
        self.head = 1
        self.tail = 1

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

    def enqueue(self, obj):
        self.q[self.tail] = obj
        if self.tail == self.size - 1:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.head == self.size - 1:
            self.head = 0
            return self.q[self.size - 1]
        else:
            self.head += 1
            return self.q[self.head - 1]


qu = Queue()

li = list(map(int, input().split()))
n = li[0]
q = li[1]

for i in range(n):
    li = list(map(str, input().split()))
    li[1] = int(li[1])
    qu.enqueue(li)








time = 0
while True:
    temp_list = qu.dequeue()
    temp_name = temp_list[0]
    temp_num = temp_list[1]
    if temp_num <= q:
        time += temp_num
        print(temp_name + ' {}'.format(time))
        if qu.is_empty():
            break
    else:
        li = [temp_name, temp_num - q]
        qu.enqueue(li)
        time += q

Solution (Python):
from collections import deque
d = deque()
time = dict()
complete = dict()
complete_time = 0
n, q = map(int, input().split())
for _ in range(n):
    process, msec = input().split()
    d.append(process)
    time[process] = int(msec)
while d:
    target_process = d.popleft()
    if time[target_process] <= q:
        complete_time += time[target_process]
        complete[target_process] = complete_time
    else:
        complete_time += q
        time[target_process] -= q
        d.append(target_process)

for key, value in complete.items():
    print(key, value)

Solution (Python):
'''
Pythonのリストはappend, popなどを末尾の要素に対して実行するのは早いが、先頭への挿入や取り出しは遅い。
collections.dequeを使えば両端に対する操作を効率的に実行可能。enqueueはappend, dequeueはpopleftを使う。
'''
from collections import deque
n, q = map(int, input().split())
L = []
for i in range(n):
    a, b = input().split()
    L.append([a, int(b)])
L = deque(L)

t = 0
while L:
    x = L.popleft()
    if x[1] > q:
        t += q
        x[1] -= q
        L.append(x)
    else:
        t += x[1]
        print(x[0], t)

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

que = []
for _ in range(n):
  name, total = raw_input().split()
  que += [[name, total]]

total = 0
answer = []

while que:
  
  item = que.pop(0)
  name, time = item[0], int(item[1])

  if time > q:
    que += [[name, time - q]]
    total += q
  else:
    total += time
    answer += [[name, str(total)]]

for w in answer:
  print ' '.join(w)

Solution (Python):
class Process:
    def __init__(self, name, time):
        self.name = name
        self.time = time

    def __str__(self):
        return str(self.time)


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

queue = []
for i in range(n):
    name, time = input().split(' ')
    queue.append(Process(name, int(time)))

idx = 0
total = 0
while len(queue) > idx:
    time = queue[idx].time
    if time > q:
        total += q
        queue[idx].time -= q
        queue.append(queue[idx])
    else:
        total += time
        print(queue[idx].name + ' ' + str(total))

    idx += 1

Solution (Python):
N,q=list(map(int,input().split()))
name=[]
time=[]
sum_time=0

for n_temp in range(N):
    na,ti=input().split()
    name.append(na)
    time.append(int(ti))

while True:

    t = time.pop(0)
    n = name.pop(0)
    
    if t-q>0:
        time.append(t-q)
        name.append(n)
        sum_time+=q
    else:
        sum_time+=t
        print(n,sum_time)

    if len(name)<=0:
        break

Solution (Python):


class Queue(object):
    def __init__(self):
        self.list = []

    def enqueue(self, x):
        self.list.append(x)
    
    def dequeue(self):
        return self.list.pop(0)
    
    def isEmpty(self):
        return len(self.list) == 0


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

    jobs = [input().split(' ') for _ in range(n)]


    for job in jobs:
        que.enqueue([job[0], int(job[1])])

    total = 0
    while not que.isEmpty():
        

        name, time = que.dequeue()

        nokori = time - q
        if nokori > 0:
            total += q
            que.enqueue([name, nokori])
        else:
            total += time
            print(name, total)




main()

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

Solution (Python):
import collections
n,q = map(int,input().split())
class deque():
  def __init__(self):
    self.a = [None]*100000
    self.head = 0
    self.tail = 0
  def append(self,e):
    self.a[self.tail] = e
    self.tail += 1
    if self.tail == 100000: self.tail = 1
  def __bool__(self):
    return self.head != self.tail
  def popleft(self):
    if self.head == self.tail:
      return None
    ret = self.a[self.head]
    self.head += 1
    if self.head == 100000: self.head = 1
    return ret

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

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

Solution (Python):
import heapq
import queue
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


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


N,LIMIT = map(int,input().split())
Q = deque()


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 > LIMIT:
        info.rest_time -= LIMIT
        current_time += LIMIT
        Q.append(info)
    else:
        current_time += info.rest_time
        print("%s %d"%(info.name,current_time))

Solution (Python):
from collections import deque
n, q = map(int, input().split())
lst = deque([])
for i in range(n):
    name, time = input().split()
    lst.append({'name': name, 'time': int(time)})
now = 0
while len(lst) > 0:
    task = lst.popleft()
    now += min(q, task['time'])
    task['time'] -= q
    if task['time'] > 0:
        lst.append(task)
    else:
        print(task['name'], now)

Solution (Python):
class Node:
    __slots__ = ["data", "next", "prev"]
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

class Queue:
    __slots__ = ["head", "tail"]
    def __init__(self):
        self.head = None
        self.tail = None
    
    def appendleft(self, data):
        node = Node(data)
        self.head, node.next = node, self.head
        if self.tail:
            node.next.prev = self.head
        else:
            self.tail = node
    
    def pop(self):
        if self.tail:
            target, self.tail = self.tail, self.tail.prev
            if self.tail:
                self.tail.next = None
            return target.data

def simulate_round_robin_scheduling(x, y):
    total_time = 0
    while y.tail:
        name, time = y.pop()
        time = int(time)
        total_time += min(time, x)
        time -= x
        if time > 0:
            y.appendleft([name, time])
        else:
            print("{} {}".format(name, total_time))
if __name__ == "__main__":
    n, q = map(int, input().split())
    processes = Queue()
    for _ in range(n):
        processes.appendleft(input().split())
    simulate_round_robin_scheduling(q, processes)

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

Solution (Python):
from collections import deque

d,q=list(map(int,input().split()))

name=deque()
time=deque()

for n in range(d):
    a,b=input().split()
    name.append(a)
    time.append(int(b))
    
ans=0
    
while name:
    n=name.popleft()
    t=time.popleft()
    if t>q:
        name.append(n)
        time.append(t-q)
        ans=ans+q
    else:
        ans=ans+t
        print(n,ans)

Solution (Python):
n,q=map(int, input().split())
queue=[]
for _ in range(n):
    name,time=map(str, input().split())
    queue.insert(0, {"name":name, "time":int(time)})

T=0
while queue != []:
    process=queue.pop()
    if process["time"] <= q:
        T+=process["time"]
        print(process["name"], T, sep=" ")
    else :
        process["time"]-=q
        queue.insert(0, process)
        T+=q

Solution (Python):
import collections

N, q = [int(item) for item in input().split()]
input_list = [[item for item in input().split()] for _ in range(N)]

que = collections.deque()
for p, time in input_list:
    que.append([p, int(time)])

finish_time = 0
while que:
    temp = que.popleft()

    if temp[1] - q <= 0:
        finish_time += temp[1]
        print(temp[0], finish_time)

    else:
        finish_time += q
        temp[1] -= q
        que.append(temp)

Solution (Python):
import sys
import math
import copy
from typing import Tuple, List, Union
input = sys.stdin.readline

class Process:
    def __init__(self, name: str, value: Union[int, str]):
        self.name = name
        self.value = int(value)
    def takes(self, quontum:int) -> Tuple[int, bool]:
        if self.value > quontum:
            self.value -= quontum
            return (quontum, True)
        else:
            return (self.value, False)

    def __repr__(self):
        return "{}-{}".format(self.name, self.value)


if __name__ == "__main__":
    queue = []
    n, q = map(int , input().split())
    for i in range(n):
        queue.append(Process(*input().split()))
    time = 0
    j = 0
    while len(queue):
        process = queue.pop(0)
        val, rest = process.takes(q)
        time += val
        if rest:
            queue.append(process)
        else:
            print(process.name,time)

Solution (Python):


class Task:
    def __init__(self,pid,rest_time):
        self.pid = pid
        self.rest_time=int(rest_time)

n,q = map(lambda x: int(x),input().split(" "))
tasks=[]
for i in range(n):
    A = input().split(" ")
    tasks.append(Task(A[0],A[1]))
    
time=0
while len(tasks)>0:
    t = tasks.pop(0)
    if t.rest_time<=q:
        time += t.rest_time
        print(t.pid,time)
    else:
        time += q
        t.rest_time -= q
        tasks.append(t)

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

Solution (Python):
class MyQueue:
    def __init__(self, max):
        self.MAX = max
        self.queue = [0] * self.MAX
        self.head = self.tail = 0
        
    def is_empty(self):
        return self.head == self.tail
        
    def is_full(self):
        return self.head == self.tail+1%self.MAX
        
    def enqueue(self, elm):
        if self.is_full():
            raise ValueError()
        
        self.queue[self.tail] = elm
        
        if self.tail+1 == self.MAX:
            self.tail = 0
        else:
            self.tail += 1
    
    def dequeue(self):
        if self.is_empty():
            raise ValueError()
        
        elm = self.queue[self.head]
        
        if self.head+1 == self.MAX:
            self.head = 0
        else:
            self.head += 1
        
        return elm

N, qt = [int(n) for n in input().split()]
queue = MyQueue(N+10)

for n in range(N):
    name, time = input().split()
    queue.enqueue([name, int(time)])

time = 0
while not queue.is_empty():
    prc = queue.dequeue()
    if prc[1] > qt:
        prc[1] -= qt
        queue.enqueue(prc)
        time += qt
    else:
        time += prc[1]
        print(prc[0], time)

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

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

Solution (Python):
class Queue:
    def __init__(self):
        self.max = int(1e6)
        self.data = [0] * self.max
        self.head = 0
        self.tail = 0
    def isEmpty(self):
        return self.head == self.tail
    def isFull(self):
        return self.head == (self.tail + 1) % self.max
    def enqueue(self, x):
        try:
            if self.isFull():
                raise Exception('Queue is full')
            self.data[self.tail] = x
            if self.tail + 1 == self.max:
                self.tail = 0
            else:
                self.tail += 1
        except Exception as e:
            print(e)
    def dequeue(self):
        try:
            if self.isEmpty():
                raise Exception('Queue is empty')
            x = self.data[self.head]
            if self.head + 1 == self.max:
                self.head = 0
            else:
                self.head += 1
            return x
        except Exception as e:
            print(e)

class Process:
    def __init__(self, name, time):
        self.name = name
        self.time = time
        self.endTime = 0

n, q = map(int, input().split())
processes = Queue()
for i in range(n):
    name, time = input().split()
    time = int(time)
    processes.enqueue(Process(name, time))

totalTime = 0
finishedProcesses = 0

while finishedProcesses < n:
    runningProcess = processes.dequeue()
    if runningProcess.time <= q:
        totalTime += runningProcess.time
        runningProcess.time = 0
        runningProcess.endTime = totalTime
        finishedProcesses += 1
        print(runningProcess.name, runningProcess.endTime)
    else:
        totalTime += q
        runningProcess.time -= q
        processes.enqueue(runningProcess)

Solution (Python):

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

from collections import deque

ls = []
for i in range(n):
    ls.append(list(input().split()))

que = deque(ls)

elapse = 0
while len(que)>0:
    name, time = que.popleft()
    time = int(time)
    if time <= q:
        elapse += time
        print('%s %d'%(name, elapse))
    else:
        elapse += q
        time -= q
        que.append([name, time])

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

Solution (Python):
N, Q = map(int, input().split())
X = []
for i in range(0, N):
    x, y = map(str, input().split())
    X.append([x, int(y)])
ans = []
time = 0
while len(X) != 1:
    tmp = X.pop(0)
    if tmp[1] <= Q:
        time += tmp[1]
        ans.append([tmp[0], time])
    else:
        time += Q
        tmp[1] -= Q
        X.append(tmp)
ans.append([X[0][0], time+X[0][1]])
for i in range(0, N):
    print(ans[i][0], ans[i][1])

Solution (Python):
n, q = [int(i) for i in input().split(" ")]

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

et = 0
result = []
while len(queue) > 0:
    job = queue.pop(0)
    et += min(job[1], q)
    newJob = (job[0], job[1]-q)
    if newJob[1] > 0:
        queue.append(newJob)
    else:
        result.append((job[0], et))

for r in result:
    print(r[0] + " " + str(r[1]))

Solution (Python):
from queue import Queue

def main():
    N, Q = map(int, input().split())
    q = Queue()
    for n in range(N):
        name, time = input().split()
        q.put((name, int(time)))
    
    timer = 0
    while not q.empty():
        name, time = q.get()
        if time > Q:
            time -= Q
            timer += Q
            q.put((name, time))
        else:
            timer += time
            print("{} {}".format(name, timer))
            
            
if __name__=="__main__":
    main()