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):
N, qms = map(int, input().split())

raw_proc = [input() for i in range(N)]
queue = [{} for i in range(N+1)]
for i in range(N):
    name, time = raw_proc[i].split()
    time = int(time)
    queue[i] = {"name": name, "time": time}

def enque(tail, p):
    queue[tail] = p
    tail = (tail+1) % (N+1)
    return tail

def deque(head):
    proc = queue[head]
    head = (head+1) % (N+1)
    return proc, head


if __name__ == '__main__':
    head = 0
    tail = N
    total_time = 0
    while head != tail:
        p, head = deque(head)
        t = min(qms, p["time"])
        p["time"] = p["time"] - t
        total_time += t
        if p["time"] > 0:
            tail = enque(tail, p)
        else:
            print("{} {}".format(p["name"], total_time))

Solution (Python):
N, qms = map(int, input().split())

raw_proc = [input() for i in range(N)]
queue = [{} for i in range(N+1)]
for i in range(N):
    name, time = raw_proc[i].split()
    time = int(time)
    queue[i] = {"name": name, "time": time}

def enque(tail, p):
    queue[tail] = p
    tail = (tail+1) % (N+1)
    return tail

def deque(head):
    proc = queue[head]
    head = (head+1) % (N+1)
    return proc, head


if __name__ == '__main__':
    head = 0
    tail = N
    total_time = 0
    while head != tail:
        p, head = deque(head)
        t = min(qms, p["time"])
        p["time"] = p["time"] - t
        total_time += t
        if p["time"] > 0:
            tail = enque(tail, p)
        else:
            print("{} {}".format(p["name"], total_time))

Solution (Python):
N, qms = map(int, input().split())

raw_proc = [input() for i in range(N)]
queue = [{} for i in range(N+1)]
limit = N+1
for i in range(N):
    name, time = raw_proc[i].split()
    time = int(time)
    queue[i] = {"name": name, "time": time}

def enque(tail, p):
    queue[tail] = p
    tail = (tail+1) % limit
    return tail

def deque(head):
    proc = queue[head]
    head = (head+1) % limit
    return proc, head


if __name__ == '__main__':
    head = 0
    tail = N
    total_time = 0
    while head != tail:
        p, head = deque(head)
        t = min(qms, p["time"])
        p["time"] = p["time"] - t
        total_time += t
        if p["time"] > 0:
            tail = enque(tail, p)
        else:
            print("{} {}".format(p["name"], total_time))

Solution (Python):
def main():
    
    n, q = input().split()
    n = int( n )
    q = int( q )
    name = []
    time = []
    for i in range(n):
        tmp_n, tmp_t = input().split()
        name.append( tmp_n )
        time.append( int( tmp_t ) )

    
    count = 0
    while n > 0:
        if time[0] > q:
            time[0] -= q
            count += q
            time.append( time[0] )
            time.pop( 0 )
            name.append( name[0] )
            name.pop( 0 )
        else :
            count += time[0]
            print( name[0], end=" " )
            print( str( count ) )
            time.pop( 0 )
            name.pop( 0 )
            n -= 1

if __name__ == '__main__':
    main()

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

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

Solution (Python):



def simu(pro, q):
    end_time = 0

    while bool(pro):
        if pro[1] > q:
            end_time += q
            pro.append(pro[0])
            pro.append(pro[1] - q)
            pro.pop(0)
            pro.pop(0)

        elif pro[1] <= q:
            end_time += pro[1]
            print('{0} {1}'.format(pro[0], end_time))
            pro.pop(0)
            pro.pop(0)


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

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

simu(pro, q)

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

Solution (Python):
n, q = [int(t) for t in input().split()]
procs = []
for i in range(n):
    name, tm = input().split()
    procs.append((name, int(tm)))

curtime = 0
while len(procs) > 0:
    queue = []
    for name, tm in procs:
        if tm <= q:
            curtime += tm
            print(name, curtime)
        else:
            curtime += q
            queue.append((name, tm - q))
    procs = queue

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

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

stime = 0
while que:
    job = que.pop(0)
    if job[1]<=q:
        stime += job[1]
        print job[0], stime
    else:
        stime += q
        job[1] -= q
        que.append(job)

Solution (Python):
import sys

class Queue:
    class Cell:
        def __init__(self, x, y = None):
            self.data = x
            self.next = y

    def __init__(self):
        self.size = 0
        self.rear = None

    def enqueue(self, x):
        if self.size == 0:
            self.rear = Queue.Cell(x)
            self.rear.next = self.rear
        else:
            new_cell = Queue.Cell(x, self.rear.next)
            self.rear.next = new_cell
            self.rear = new_cell
        self.size += 1

    def dequeue(self):
        if self.size == 0: raise IndexError
        front = self.rear.next
        self.rear.next = front.next
        self.size -= 1
        if self.size == 0: self.rear = None
        return front.data

    def isEmpty(self):
        return self.size == 0

def generator(n1, n2, step=1):
    n = n1
    while n<n2:
        yield n
        n+= step

n, q = map(int, raw_input().split())
 
A = Queue()
for i in generator(0,n):
    name, time = raw_input().split()
    A.enqueue([name, int(time)])

stime = 0
while not A.isEmpty():
    job = A.dequeue()
    if q>=job[1]:
        stime += job[1]
        print job[0], stime
    else:
        stime += q
        job[1] -= q
        A.enqueue(job)

Solution (Python):
import sys

class Queue:
    class Cell:
        def __init__(self, x, y = None):
            self.data = x
            self.next = y

    def __init__(self):
        self.size = 0
        self.rear = None

    def enqueue(self, x):
        if self.size == 0:
            self.rear = Queue.Cell(x)
            self.rear.next = self.rear
        else:
            new_cell = Queue.Cell(x, self.rear.next)
            self.rear.next = new_cell
            self.rear = new_cell
        self.size += 1

    def dequeue(self):
        if self.size == 0: raise IndexError
        front = self.rear.next
        self.rear.next = front.next
        self.size -= 1
        if self.size == 0: self.rear = None
        return front.data

    def isEmpty(self):
        return self.size == 0

def generator(n1, n2, step=1):
    n = n1
    while n<n2:
        yield n
        n+= step

n, q = map(int, raw_input().split())
TASK=[]
A = Queue()
for i in generator(0,n):
    name, time = raw_input().split()
    TASK.append([name, int(time)])
    A.enqueue(i)

stime = 0
while not A.isEmpty():
    p = A.dequeue()
    if q>= TASK[p][1]:
        stime += TASK[p][1]
        print TASK[p][0], stime
    else:
        stime += q
        TASK[p][1] -= q
        A.enqueue(p)

Solution (Python):
import collections

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

datalist = collections.deque(datalist)

time = 0
while len(datalist):
	data = datalist.popleft()
	data[1] = int(data[1])
	
	if data[1] <= q:
		time += data[1]
		print data[0], time
		
	else:
		time += q
		data[1] = data[1] - q
		datalist.append(data)

Solution (Python):

from __future__ import division, print_function
from sys import stdin

num, qt = (int(s) for s in stdin.readline().split())
rrs = []
for _ in range(num):
    name, time = stdin.readline().split()
    rrs.append([name, int(time)])

process = 0
while rrs:
    rrs[0][1] -= qt
    if rrs[0][1] <= 0:
        process += qt + rrs[0][1]
        print(rrs.pop(0)[0], process)
    else:
        process += qt
        p = rrs.pop(0)
        rrs.append(p)

Solution (Python):



import time
import sys
import io
import re
import math
from Queue import Queue
import collections
start = time.clock()
i = 0
(n,q)=map(int, raw_input().split())
datalist=[raw_input().split() for _ in range(n)]
datalist=collections.deque(datalist)

while len(datalist):
    data=datalist.popleft()

    data[1]=int(data[1])
    if data[1]<=q:
        i+=data[1]
        print data[0],i
    else:
        i+=q
        data[1]-=q
        datalist.append(data)

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
t = 0
while que:
	a = que.pop(0)
	if int(a[1]) <= q:
		t += int(a[1])
		print a[0],t				
	else:
		t += q
		que.append([a[0],int(a[1])-q])

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	a = que[i]
	if 0 < a[1] <= q:
		t += a[1]
		que.pop(i)
		n -= 1
		print a[0],t				
	elif q < a[1]:
		t += q
		que[i][1] = a[1] - q
		i += 1

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	a,c = que[i][0],que[i][1]
	if c <= q:
		t += c
		que.pop(i)
		n -= 1
		print a,t				
	else:
		t += q
		que[i][1] = c - q
		i += 1

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if que[i][1] <= q:
		t += que[i][1]
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] -= q
		i += 1

Solution (Python):
n,q = map(int, raw_input().split())
que = []
for i in range(n):
	inp = map(str, raw_input().split())
	que.append([inp[0],int(inp[1])])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if que[i][1] <= q:
		t += que[i][1]
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] -= q
		i += 1

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
mxt = sum([int(y) for x,y in que])
t = 0
i = 0
while t < mxt:
	i %= n
	if int(que[i][1]) <= q:
		t += int(que[i][1])
		print que[i][0],t
		que.pop(i)
		n -= 1
	else:
		t += q
		que[i][1] = int(que[i][1]) - q
		i += 1

Solution (Python):
n,q = map(int, raw_input().split())
que = [map(str, raw_input().split()) for i in range(n)]
for i in range(n):que[i][1] = int(que[i][1])
mxt = sum([y for x,y in que])
t = 0
i = 0
while t < mxt:
    if i >= n: i %= n
    if que[i][1] <= q:
        t += que[i][1]
        print que[i][0],t
        que.pop(i)
        n -= 1
    else:
        t += q
        que[i][1] -= q
        i += 1

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

Solution (Python):
sum=0
s=0
A=input().split()
n=(int)(A[0])
m=(int)(A[1])
x=[0 for i in range(2*n)]
for i in range(n):
    B=input().split()
    k=2*i
    x[k]=(B[0])
    x[k+1]=(int)(B[1])
while len(x)>0:
    s=x[1]-m
    if s>0:
        sum+=m
        x.append(x[0])
        x.append(s)
        del x[0]
        del x[0]
    if s<=0:
        sum+=x[1]
        print( x[0],sum)
        del x[0]
        del x[0]

Solution (Python):
from collections import deque

n, q = map(int, input().split())
listA = []
t=1
while t <=n:  
    try:
        listA.append(input().split())
        t +=1
    except:
        break

result=[]
qu = deque()
for i in range(n):
    qu.append(listA[i]) 

time = 0    
while True:
    try:
        task = qu.popleft()     
        if int(task[1]) <= q:   
            time += int(task[1]) 
            result.append([task[0], time]) 
        else:
            time += q 
            task[1] = int(task[1]) - q 
            qu.append(task) 
    except:
        break

for i in range(n):
    print(*result[i])

Solution (Python):
N, Q = map(int, input().split())
from collections import deque
queue = deque([])

time = 0
for _ in range(N):
    name, t = input().split()
    t = int(t)
    queue.append([name, t])

while queue:
    process = queue.popleft()
    red = min(Q, process[1])
    time += red
    process[1] -= red
    if process[1] == 0:
        print(process[0], time)
    else:
        queue.append(process)

Solution (Python):
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:
    a = queue.popleft()
    time = a[1]
    name = a[0]

    if time > q:
        time -= q
        queue.append([name, time])
        t += q
    
    else:
        t += time
        print(a[0],t)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from collections import deque
n, q = MI()
A = []
for i in range(n):
    name, time = input().split()
    A.append((name, int(time)))

queue = deque(A)
time_end = 0
ans_list = []
while len(queue) != 0:
    a = queue.popleft()
    if a[1] <= q:
        time_end += a[1]
        ans_list.append((a[0], time_end))
    else:
        time_end += q
        queue.append((a[0], a[1] - q))

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

Solution (Python):
head = 0
tail = 0
max_i = 200000
elapsed_time = 0

def initialize():
  global head, tail
  head = tail = 0

def isEmpty():
  global head, tail
  return head == tail

def isFull():
  global head, tail, max_i
  return head == (tail + 1) % max_i

def enqueue(x):
  if isFull():
    print('over flow')
  global head, tail
  Q[tail] = x
  if tail + 1 == max_i:
    tail = 0
  else:
    tail += 1

def dequeue():
  if isEmpty():
    print('under flow')
  global head, tail
  x = Q[head]
  if head + 1 == max_i:
    head = 0
  else:
    head += 1
  return x

initialize()

N, q = map(int, input().split())
max_i = N + 2
Q = [('', 0) for i in range(max_i)]
for i in range(N): 
  Q[i] = list(input().split())
  Q[i][1] = int(Q[i][1])
  tail += 1

while (head != tail): 
  task = dequeue()
  count = min(q, task[1])
  task[1] -= count
  elapsed_time += count
  if task[1] > 0:
    enqueue(task)
  else:
    print(f"{task[0]} {elapsed_time}")

Solution (Python):
class queue():
    def __init__(self):
        self.Q = []
    
    def enqueue(self, x):
        self.Q.append(x)
        
    def dequeue(self):
        return self.Q.pop(0)
    
    def isEmpty(self):
        if len(self.Q) == 0:
            return True
        else:
            return False
    
n, q = map(int, input().split())
RRS = queue()
for _ in range(n):
    prcs = input().split()
    RRS.enqueue([prcs[0], int(prcs[1])])

time = 0
while True:
    if RRS.isEmpty():
        break
    prcs = RRS.dequeue()
    c = min(prcs[1], q)
    prcs[1] -= c
    time += c
    if prcs[1] > 0:
        RRS.enqueue(prcs)
    else:
        print(prcs[0], time)

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

Solution (Python):
from collections import deque
n, q = map(int, input().split())
process = deque([tuple(map(str, input().split())) for _ in range(n)])

answer = []
time_count = 0
while process:
    name, time = process.popleft()
    time = int(time)

    if time - q > 0:
        time_count += q
        process.append((name, time - q))
    else:
        time_count += time
        answer.append((name, time_count))

for process in answer:
    name, time = process
    print(name, time)

Solution (Python):
from collections import deque

N,q = map(int,input().split())
stack = deque()

stack.extend(list(input().split() for i in range(N)))
cnt = 0
endtime = deque()
while True:
    if len(stack) == 0:
        break
    x = stack.popleft()
    x[1] = int(x[1])
    if x[1] <= q:
        cnt += x[1]
        endtime.append([x[0],cnt])
    else:
        cnt += q
        x[1] -= q
        stack.append(x)



for i in endtime:
    print("{} {}".format(i[0],i[1]))

Solution (Python):
class Queue:
    def __init__(self) -> None:
        self.queue = list()

    def _is_empty(self):
        if len(self.queue) == 0:
            return True
        return False

    def enqueue(self, obj):
        self.queue.append(obj)

    def dequeue(self):
        if self._is_empty():
            return None
        else:
            return self.queue.pop(0)

    def __len__(self):
        return len(self.queue)


def main():
    n, q = map(int, input().split(' '))
    queue = Queue()
    for i in range(n):
        name, num = input().split(' ')
        queue.enqueue({'name': name, 'num': int(num)})
    time = 0
    while True:
        if len(queue) == 0:
            break
        task = queue.dequeue()
        task['num'] -= q
        if task['num'] > 0:
            queue.enqueue(task)
            time += q
        else:
            time += q+task['num']
            print(task['name'], time)


if __name__ == "__main__":
    main()

Solution (Python):
def Initialize(head, tail):
    head, tail = 0, 0
    return head, tail

def IsEnpty(head, tail):
    return head == tail

def IsFull(head, tail):
    global MAX
    return head == (tail + 1) % MAX

def Enqueue(Q, x, y, head, tail):
    global MAX
    if IsFull(head, tail):
        return None
    Q[tail][0], Q[tail][1] = x, y
    if tail + 1 == MAX:
        tail = 0
    else:
        tail += 1
    return head, tail

def Dequeue(Q, head, tail):
    global MAX
    if IsEnpty(head, tail):
        return None
    x, y = Q[head][0], Q[head][1]
    if head + 1 == MAX:
        head = 0
    else:
        head += 1
    return x, y, head, tail

n, q = map(int, input().split())
MAX = n + 2
Q = [['', 0] for _ in range(MAX)]
head, tail = 0, 0
result = []
z = 0
for i in range(n):
    x, y = input().split()
    head, tail = Enqueue(Q, x, int(y), head, tail)
while IsEnpty(head, tail) != True:
    if Q[head][1] <= q:
        x, y, head, tail = Dequeue(Q, head, tail)
        result.append([x, y + z])
        z += y
    else:
        Q[head][1] -= q
        x, y, head, tail = Dequeue(Q, head, tail)
        head, tail = Enqueue(Q, x, y, head, tail)
        z += q
for i in range(n):
    print(*result[i], sep=' ')

Solution (Python):
n, q = map(int, input().split())
li = []
for i in range(n):
    p, ms = map(str, input().split())
    li.append([p, int(ms)])
t = 0
out = []
while True:
    if(len(out)==n):
        break
    a = li.pop(0)
    p, ms = a[0], a[1]
    if(ms <= q):
        t += ms
        out.append([p, t])
    else:
        ms -= q
        t += q
        li.append([p, ms])

for i in range(n):
    print(out[i][0], out[i][1])

Solution (Python):
n, q = map(int, input().split())
P = []
T = []
for i in range(n):
    p, t = map(str, input().split())
    P.append(p)
    T.append(int(t))

EndP = []
EndT = []
time = 0
while True:
    if T==[]:
        break
    
    if T[0]>q:
        T.append(T[0]-q)
        T.pop(0)
        P.append(P[0])
        P.pop(0)
        time += q
    
    if T[0]<=q:
        EndP.append(P[0])
        P.pop(0)
        time += T[0]
        EndT.append(time)
        T.pop(0)

for i in range(n):
    print(EndP[i],EndT[i])

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

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

count = 0

while Queue:
    name = Queue[0][0]
    time = Queue[0][1]

    if time > q:
        Queue[0][1] -=q
        Queue.append(Queue.pop(0))

        count += q
    else:
        print(name,time+count)
        Queue.pop(0)
        count += time

Solution (Python):
from collections import deque


def main():
    n, q = [int(i) for i in input().split()]
    P = [input().split() for i in range(n)]
    process = deque()
    process.extend([
        {'name': p[0], 'time': int(p[1])}
        for p in P
    ])
    sum_time = 0
    while len(process):
        p = process.popleft()
        if p['time'] <= q:
            sum_time += p['time']
            print(p['name'], sum_time)
        else:
            sum_time += q
            p['time'] -= q
            process.append(p)


if __name__ == "__main__":
    main()

Solution (Python):
LEN = 100005

n, q = map(int, input().split())
name = [None] * LEN
time = [None] * LEN

for i in range(n):
  name[i], time[i] = map(str, input().split())

head = 0
tail = n
curr = 0
done = 0

while True:
  if int(time[head]) > q:
    name[tail] = name[head]
    time[tail] = int(time[head]) - q
    curr += q

    if LEN == head + 1:
      head = 0
    else:
      head += 1

    if LEN == tail + 1:
      tail = 0
    else:
      tail += 1
    
  else:
    
    done += 1
    curr += int(time[head])
    print("{} {}".format(name[head], curr))

    if (done == n):
      break

    if LEN == head + 1:
      head = 0
    else:
      head += 1

Solution (Python):


class queue:
    def __init__(self, length):
        self.max = length + 2
        self.v = list(range(self.max))
        self.head = 0
        self.tail = 0
        
    def enqueue(self, x):
        self.v[self.tail] = x
        self.tail = (self.tail+1) % self.max
        
    def dequeue(self):
        a = self.v[self.head]
        self.head = (self.head+1) % self.max
        
        return a
    
    
    def isEmpty(self):
        return self.head == self.tail
    
    def isFull(self):
        return (self.tail - self.head) == self.length
    
    
def main():
    n, q = list(map(int,input().split()))
    que = queue(n)
    for _ in range(n):
        lis = input().split()
        lis[1] = int(lis[1])
        
        que.enqueue(lis)
    
    time = 0
    while n > 0:
        now = que.dequeue()
        if now[1] > q:
            now[1] -= q
            que.enqueue(now)
            time += q
            
        else:
            time += now[1]
            print(now[0], time)
            n -= 1
            
if __name__ == '__main__':
    main()

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

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

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

Solution (Python):
class queue:
    def __init__(self,queue_size):
        self.size = queue_size
        self.queue = [None] * self.size
        self.head = 0
        self.tail = 0

    def isEmpty(self):
        return self.head == self.tail 
    
    def isFull(self):
        return self.head == (self.tail +1)%self.size

    def enqueue(self, x):
        self.queue[self.tail] = x
        if self.tail + 1 == self.size:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        x = self.queue[self.head]
        if self.head + 1 == self.size:
            self.head = 0
        else:
            self.head += 1

        return x

num = 100000
que = queue(num)
time = 0

n, q = map(int, input().split())
for i in range(n):
    p, t = input().split()
    que.enqueue([int(t),p])

while not que.isEmpty():
    pt = que.dequeue()
    if pt[0] > q:
        time += q
        pt[0] -= q
        que.enqueue(pt)
    
    else:
        time += pt[0]
        print(pt[1], time)

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

Solution (Python):
a,b=map(int, input().split())
s=[]
for i in range(a):
    name, time = input().split()
    time = int(time)
    s.append([name,time])
time=0
while True:
    if len(s)==0:
        break
   
    if s[0][1]>b:
        s[0][1]-=b
        s.append(s[0])
        s.pop(0)
        time+=b
    else:
        time=time+s[0][1]
        print("{} {}".format(s[0][0],time))
        s.pop(0)

Solution (Python):
import sys
MAX = 100005
n, q = map(int, input().split())

def initialize():
    head = tail = 0
    return head, tail

def isEmpty(head, tail):
    return head == tail

def isFull(head, tail):
    return head == (tail + 1) % MAX

def enqueue(x, head, tail):
    if isFull(head, tail):
        print('Error')
        sys.exit()
    Q[tail] = x
    if tail + 1 == MAX:
        tail = 0
    else:
        tail += 1
    return tail

def dequeue(head, tail):
    if isEmpty(head, tail):
        sys.exit()
    x = Q[head]
    if head + 1 == MAX:
        head = 0
    else:
        head += 1
    return x, head

head, tail = initialize()
Q = [0] * MAX
for _ in range(n):
    name, time = map(str, input().split())
    time = int(time)
    tail = enqueue([name, time], head, tail)
T = 0
while True:
    Qi, head = dequeue(head, tail)
    name = Qi[0]
    time = Qi[1]
    if time <= q:
        T += time
        print(name, T)
    else:
        T += q
        time -= q
        tail = enqueue([name, time], head, tail)

Solution (Python):
from collections import deque 

number_of_execution, rotation_time = map(int, input().split())
queue = deque([])  

for i in range(number_of_execution):
    process_name, execution_time = input().split()
    time = int(execution_time)
    queue.append([process_name, time])

passed_time = 0
while len(queue) > 0:
    running_process = queue.popleft()
    passed_time += min(running_process[1], rotation_time)
    running_process[1] -= rotation_time
    if running_process[1] > 0:
        queue.append(running_process)
    else:
        print('{} {}'.format(running_process[0], passed_time))

Solution (Python):
class Node:
    def __init__(self, val, next_node = None):
        self.value = val
        self.next = next_node
        
class que:
    def __init__(self):
        self.start = None
        self.last = None
    def append(self, num):
        new_node = Node(num)
        if self.start == None:
            self.start = new_node
            self.last = new_node
        else:
            self.last.next = new_node
            self.last = new_node
    def popleft(self):
        return_node = self.start
        self.start = return_node.next
        return return_node.value
    def isempty(self):
        return self.start == None
    def print_vals(self):
        node = self.start
        output =""
        while node != None:
            output += str(node.value)
            node = node.next
        print(output)
        
       
n, p = map(lambda x:int(x), input().split(' '))
processes = que()
for i in range(n):
    name, time = input().split(' ')
    time = int(time)
    processes.append((name, time))
all_time = 0
while not processes.isempty():
    name, time = processes.popleft()
    if time <= p:
        all_time += time
        print(f'{name} {all_time}')
    else:
        all_time += p
        processes.append((name, time - p))

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

Solution (Python):
from collections import deque

n, q = map(int, input().split())
que = deque([])
for _ in range(n):
    key, value = input().split()
    que.append([key, int(value)])
    
t = 0
que_2 = []
while True:
    
    if que[0][1] > q:
        t += q
        new = [que[0][0], que[0][1] - q]
        que.popleft()
        que.append(new)
    else:
        t += que[0][1]
        new_2 = [que[0][0], t]
        que_2.append(new_2)
        que.popleft()
        if len(que) == 0:
            break
        
for i in range(n):
    print(que_2[i][0], que_2[i][1])