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,q = map(int,input().split())
process = []
for i in range(n):
    name,time = input().split()
    process.append([name,int(time)])
elap = 0
out = []
while len(process) > 0:
    name,time = process.pop(0)
    temp_q = q
    while True:
        if time <= 0:
            out.append([name,elap])
            break
        if temp_q <= 0:
            process.append([name,time])
            break
        time -= 1
        temp_q -= 1
        elap += 1
for name,time in out:
    print(name,time)

Solution (Python):
n,q = map(int,input().split())
process = []
for i in range(n):
    name,time = input().split()
    process.append([name,int(time)])
elap = 0
out = []
while len(process) > 0:
    name,time = process.pop(0)
    temp_q = q
    while True:
        if time <= 0:
            out.append([name,elap])
            break
        if temp_q <= 0:
            process.append([name,time])
            break
        time -= 1
        temp_q -= 1
        elap += 1
for name,time in out:
    print(name,time)

Solution (Python):
from collections import deque as D

n, q = map(int, input().split())
dq = D()
for i in range(n):
    p, t = map(str, input().split())
    dq.append([p, int(t)])
total = 0


while dq:
    pt = dq.popleft()
    if pt[1]<=q:
        total += pt[1]
        print(pt[0], total)
    else:
        total += q
        dq.append([pt[0],pt[1]-q])

Solution (Python):
sum=0
s=0
A=input().split()
n=(int)(A[0])
t=(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]-t
    if s>0: 
        sum+=t
        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):
a=input().split()
n=(int)(a[0])
q=(int)(a[1])
b=[0 for i in range(2*n)]
cnt=0
tmp=0
for i in range(n):
    B=input().split()
    b[2*i]=B[0]
    b[2*i+1]=int(B[1])
while len(b)>0:
    tmp=b[1]-q
    if tmp>0:
        b.append(b[0])
        b.append(tmp)
        del b[0]
        del b[0]
        cnt=cnt+q
    else:
        cnt=cnt+b[1]
        print(b[0],cnt)
        del b[0]
        del b[0]

Solution (Python):
from queue import Queue

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

qu=Queue()  

for i in range(n):
    name,t=input().split()
    qu.put((name,int(t))) 
    

cnt=0

while qu.qsize()>0: 
    name,t=qu.get() 
    cnt+=min(q,t)  
    if q< t:
        qu.put((name,t-q))  
    else:
        print(name,cnt)

Solution (Python):
n,q=list(map(int,input().split()))
name=[]
time=[]
sum=0
for i in range(n):

	na,ti=input().split()
	name.append(na)
	time.append(int(ti))
	
	
while len(name)>0:
	na=name.pop(0)
	ti=time.pop(0)
	
	if ti>q:
		sum+=q
		name.append(na)
		time.append(ti-q)
		
	else:
		sum+=ti
		print(na, sum)

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

    def is_empty(self):
        return self.num_items == 0

    def is_full(self):
        return self.num_items == self.MAX

    def enqueue(self, x):
        if self.is_full():
            raise IndexError

        self.queue[self.tail] = x
        self.num_items += 1
        if self.tail+1 == self.MAX:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            raise IndexError

        x = self.queue[self.head]
        self.num_items -= 1
        if self.head+1 == self.MAX:
            self.head = 0
        else:
            self.head += 1

        return x


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

    def print_time(self):
        return self.time

    def print_name(self):
        return self.name


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

    t = 0
    for i in range(n):
        name, time = input().split()
        p = Process(name, int(time))
        qq.enqueue(p)

    while not qq.is_empty():
        p = qq.dequeue()

        if p.time - q <= 0:
            t += p.time
            print("{} {}".format(p.name, t))
        else:
            p.time -= q
            t += q
            qq.enqueue(p)


if __name__ == '__main__':
    main()

Solution (Python):
from queue import deque

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

    task_q = deque()
    for idx in range(n):
        l = input().split(' ')
        task_q.append((l[0], int(l[1])))

    tot_time = 0
    while len(task_q) != 0:

        task = task_q.popleft()
        if task[1] > q:
            task_q.append((task[0], task[1] - q))
            tot_time += q

        else:
            tot_time += task[1]
            print(task[0], tot_time)


main()

Solution (Python):
class Data:
    def __init__(self):
        self.name = ""
        self.time = -1
    def printdata(self,sum):
        print(self.name,sum)
    def datain(self,name,time):
        self.name = name
        self.time = int(time)

def enqueue(Q,x):
    global tail
    Q[tail] = x
    if(tail+1 == n+1):
        tail = 0
    else:
        tail += 1

def dequeue():
    global head
    if(head+1 == n+1):
        head = 0
    else:
        head += 1

n,q = map(int,input().split())
A = [Data()]
head = 0
tail = n
sum = 0

for i in range(n):
    d = list(map(str,input().split()))
    A.append(Data())
    A[i].datain(d[0],d[1])
A.append(Data()) 

while(head != tail):
    if(A[head].time - q > 0):
        A[head].time -= q
        enqueue(A,A[head])
        dequeue()
        sum += q
    else:
        sum += A[head].time
        A[head].printdata(sum)
        dequeue()

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):
n, q = map(int, input().split())
input_list = []
for i in range(n):
    name, time = input().split()
    input_list.append([str(name), int(time)])
queue_head = 0
now_time = 0
while queue_head != len(input_list):
    if q < input_list[queue_head][1]:
        input_list.append([input_list[queue_head][0], input_list[queue_head][1] - q])
        queue_head += 1
        now_time += q
    else:
        now_time += input_list[queue_head][1]
        print(input_list[queue_head][0], now_time)
        queue_head += 1

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

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

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

Solution (Python):

All = 0
q = []
Queue = []
tmp,time = map(int,input().split())
for i in range(tmp):
    p,zi =input().split()
    q.append([p,int(zi)])
while len(q) > 0:
    if q[0][1]<= time:
        All+=q[0][1]
        v=q.pop(0)
        Queue.append([v[0],All])
    else:
        All+=time
        q[0][1]-=time
        last=q.pop(0)
        q.append(last)
for i in range(len(Queue)):
    print("{0} {1}".format(Queue[i][0],Queue[i][1]))

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

Solution (Python):
n,q = map(int,raw_input().split(" "))
task = [raw_input().split(" ") for i in range(n)]
c= 0
while task:
    name,time = task.pop(0)
    e = int(time) - q
    if e > 0:
        c+= q
        task.append([name,e])
    else:
        c+= int(time)
        print name+" "+str(c)

Solution (Python):
from collections import namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail -= self.n

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        else:
            e = self._l[self._head]
            self._l[self._head] = None
            self._head += 1
            if self._head > self.n:
                self._head -= self.n
            return e

    def isEmpty(self):
        return self._head == self._tail

    def isFull(self):
        return self._tail == self.n

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n+1)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))

Solution (Python):
from collections import namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail = 0

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        else:
            e = self._l[self._head]
            self._l[self._head] = None
            self._head += 1
            if self._head > self.n:
                self._head = 0
            return e

    def isEmpty(self):
        return self._head == self._tail

    def isFull(self):
        return self._tail == self.n

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))

Solution (Python):
from collections import namedtuple

Task = namedtuple('Task', 'name time')

class Queue:
    def __init__(self, n):
        self.n = n
        self._l = [None for _ in range(self.n + 1)]
        self._head = 0
        self._tail = 0

    def enqueue(self, x):
        if self.isFull():
            raise IndexError("queue is full")
        self._l[self._tail] = x
        self._tail += 1
        if self._tail > self.n:
            self._tail = 0

    def dequeue(self):
        if self.isEmpty():
            raise IndexError("pop from empty queue")
        e = self._l[self._head]
        self._l[self._head] = None
        self._head += 1
        if self._head > self.n:
            self._head = 0
        return e

    def isEmpty(self):
        return self._head == self._tail

    def isFull(self):
        return self._head == (self._tail + 1) % (self.n + 1)

if __name__ == '__main__':
    n, q = map(int, input().split())
    queue = Queue(n)
    for _ in range(n):
        name, time = input().split()
        time = int(time)
        queue.enqueue(Task(name=name, time=time))

    now = 0
    while not queue.isEmpty():
        task = queue.dequeue()
        t = task.time
        if t <= q:
            now += t
            print(task.name, now)
        else:
            now += q
            queue.enqueue(Task(task.name, t - q))

Solution (Python):
from collections import deque

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

que = deque(ps)
time = 0

while que:

    p, rest = que.popleft()

    elapsed = min(q, int(rest))
    time += elapsed
    rest = str(int(rest) - elapsed)

    if int(rest) > 0:
        que.append([p, rest])
    else:
        print(p, time)

Solution (Python):
n, q = map(int, input().split(' '))
p = []
time = 0
counter = 0
for i in range(n):
    tmp_n, tmp_p = input().split(' ')
    tmp_p = int(tmp_p)
    p.append((tmp_n, tmp_p))

while len(p) > 0:
    task = p.pop(0)
    tmp_p = task[1] - q
    if( tmp_p > 0):
        time += q
        p.append((task[0], tmp_p))
    elif( tmp_p <= 0):
        time += task[1]
        print('{} {}'.format(task[0], time))

Solution (Python):
from collections import deque

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

end_time = 0
while len(a):
    e = a.popleft()
    if int(e[1]) - q > 0:
        e[1] = int(e[1]) - q
        a.append(e)
        end_time += q
    else:
        end_time += int(e[1])
        print(e[0], end_time)

Solution (Python):
from collections import deque
n, q = map(int, input().split())
queue = deque()
for i in range(n):
    name, time = input().split()
    queue.append({"name": name, "time": int(time)})
t = 0
while len(queue) != 0:
    
    head = queue.popleft()
    if head["time"] <= q:
        t += head["time"]
        name = head["name"]
        print("{} {}".format(name, t))
    else:
        queue.append({"name": head["name"], "time": head["time"] - q})
        t += q

Solution (Python):
from collections import deque

n, q = [int(_) for _ in input().split()]
processes = deque([tuple(input().split()) for i in range(n)])

time = 0
while processes:
    process = processes.popleft()

    if int(process[1]) <= q:
        time += int(process[1])
        print(process[0], time)

    else:
        time += q
        processes.append((process[0], int(process[1]) - q))

Solution (Python):



def main():
    data = input().split()
    n = int(data[0]); q = int(data[1])
    list = [] 
    for i in range(n):
        list.append(input().split())
        list[i][1] = int(list[i][1])
    t = 0
    while len(list) > 0:
        if list[0][1] > q:
            name = list[0][0]; time = list[0][1]
            del list[0]
            list.append([name, time - q])
            t += q
        else:
            t += list[0][1]
            print(list[0][0] + " " + str(t))
            del list[0]

if __name__ == "__main__":
    main()

Solution (Python):

n, t_u = map(int,input().split())
name_pro_dict = {}
for i in range(n):
    name, pro = input().split()
    name_pro_dict[name] = int(pro)

t = 0
while name_pro_dict:
    keys = list(name_pro_dict.keys())
    for key in keys:
        if name_pro_dict[key] <= t_u:
            t += name_pro_dict[key]
            print(key, t)
            del name_pro_dict[key]
        else:
            name_pro_dict[key] -= t_u
            t += t_u

Solution (Python):
n, q = [int(i) for i in input().split()]
que = []
ans_count = 0 
time = 0 

for i in range(n):
    que.append([i for i in input().split()])

while(ans_count!=n):
    pop_process = que.pop(0)
    pop_process[1] = int(pop_process[1])
    
    if pop_process[1] <= q:
        time += pop_process[1]
        print('{0} {1}'.format(pop_process[0], time))
        ans_count += 1
    else:
        time += q
        pop_process[1] -= q
        que.append(pop_process)

Solution (Python):
import queue


nq = input().split()
n = int(nq[0])
q = int(nq[1])

qu = queue.Queue()
for _ in range(n):
    nt = input().split()
    qu.put({"name": nt[0], "time": int(nt[1])})

t = 0
while not(qu.empty()):
    process = qu.get()
    if process["time"] > q:
        process["time"] -= q
        t += q
        qu.put(process)
    else:
        t += process["time"]
        print(process["name"], t)

Solution (Python):




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

    def enqueue(self, x):
        self.l.append(x)

    def dequeue(self):
        x = self.l[0]
        del self.l[0]
        return x

    def is_empty(self):
        return len(self.l) == 0


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


if __name__ == '__main__':
    n, q = map(int, raw_input().split())
    Q = Queue()
    i = 0
    while i != n:
        tmp = raw_input().split()
        Q.enqueue(Process(tmp[0], int(tmp[1])))
        i += 1
    cnt = 0
    while not Q.is_empty():
        p = Q.dequeue()
        if p.time > q:
            p.time -= q
            cnt += q
            Q.enqueue(p)
        else:
            cnt += p.time
            print p.name, cnt

Solution (Python):
from collections import deque, namedtuple

Process = namedtuple('Process', 'name, time')

def main():
    n, q = map(int, input().split())
    dq = deque()
    for _ in range(n):
        name, time = input().split()
        dq.append(Process(name, int(time)))
    cur_time = 0
    while len(dq) > 0:
        x = dq.popleft()
        if x.time > q:
            dq.append(Process(x.name, x.time-q))
            cur_time += q
        else:
            cur_time += x.time
            print(x.name, cur_time)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
def stdinput():
    return stdin.readline().strip()

from collections import deque

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

    ctime = 0
    while len(ps) > 0:
        p = ps.popleft()
        if p[1] <= q:
            ctime += p[1]
            print(f'{p[0]} {ctime}')
        else:
            ctime += q
            p[1] -= q
            ps.append(p)

if __name__ == '__main__':
    main()

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

queue = []

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

time = 0
ans = []

while queue:
    tmp = queue.pop(0)
    if tmp[1] > q:
        tmp[1] -= q
        time += q
        queue.append(tmp)
    else:
        time += tmp[1]
        ans.append((tmp[0],time))

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

Solution (Python):
from queue import Queue

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

	def set_time(self, time):
		self.time = time

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

time_elapsed = 0
while queue.empty() != True:
	process = queue.get()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		queue.put(process)
	else:
		time_elapsed += min(time, q)
		print(process.name + ' ' + str(time_elapsed))

Solution (Python):
from queue import Queue

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

	def set_time(self, time):
		self.time = time

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

time_elapsed = 0
while queue.empty() != True:
	process = queue.get()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		queue.put(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

Solution (Python):

class Node:
	def __init__(self, value):
		self.value = value
		self.next = None

class SimpleQeueue:
	def __init__(self):
		self.size = 0
		self.head = None
		self.tail = None
		return

	def enqueue(self, value):
		node = Node(value)
		if self.head == None:
			self.head = node
			self.tail = node
		else:
			self.tail.next = node
			self.tail = node
		self.size += 1

	def dequeue(self):
		if self.is_empty() == True:
			return None
		node = self.head
		self.head = self.head.next
		self.size -= 1
		if self.is_empty() == True:
			self.head = None
			self.tail = None
		return node.value

	def peek(self):
		if self.is_empty() == True:
			return None
		return self.head.value

	def get_size(self):
		return self.size

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

	def __str__(self):
		str_ = '['
		current = self.head
		while current != None:
			str_ += str(current.value)
			current = current.next
			if current != None:
				str_ += ', '
		str_ += ']['
		if self.tail != None:
			str_ += str(self.tail.value)
		str_ += ']'
		return str_

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

	def set_time(self, time):
		self.time = time

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

time_elapsed = 0
while myqueue.is_empty() != True:
	process = myqueue.dequeue()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		myqueue.enqueue(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

Solution (Python):

class Node:
	def __init__(self, value):
		self.value = value
		self.next = None

class SimpleQueue:
	def __init__(self):
		self.size = 0
		self.head = None
		self.tail = None
		return

	def enqueue(self, value):
		node = Node(value)
		if self.head == None:
			self.head = node
			self.tail = node
		else:
			self.tail.next = node
			self.tail = node
		self.size += 1

	def dequeue(self):
		if self.is_empty() == True:
			return None
		node = self.head
		self.head = self.head.next
		if self.head == None:
			self.tail = None
		self.size -= 1
		return node.value

	def peek(self):
		if self.is_empty() == True:
			return None
		return self.head.value

	def get_size(self):
		return self.size

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

	def __str__(self):
		str_ = '['
		current = self.head
		while current != None:
			str_ += str(current.value)
			current = current.next
			if current != None:
				str_ += ', '
		str_ += ']['
		if self.tail != None:
			str_ += str(self.tail.value)
		str_ += ']'
		return str_

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

	def set_time(self, time):
		self.time = time

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

time_elapsed = 0
while myqueue.is_empty() != True:
	process = myqueue.dequeue()
	time = process.time
	if time > q:
		process.set_time(time - q)
		time_elapsed += q
		myqueue.enqueue(process)
	else:
		time_elapsed += time
		print(process.name + ' ' + str(time_elapsed))

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

    def exec(self, q):
        if self.s <= q:
            time = self.s
            self.s = 0
            return time
        else:
            time = q
            self.s = self.s - q
            return time

def execute(processes, q):
    time = 0
    completed = []
    while processes:
        p = processes.pop()
        time += p.exec(q)
        if p.s == 0:
            completed.append((p, time))
        else:
            processes.insert(0, p)
    return completed

def run():
    n, q = list(map(int, input().split()))
    processes = []
    for _ in range(n):
        name, time = input().split()
        processes.append(Process(name, int(time)))
    processes.reverse()
    completed = execute(processes, q)
    for c in completed:
        print(c[0].name, c[1])

if __name__ == '__main__':
    run()

Solution (Python):
class ProcessQueue:
    def __init__(self, N):
        self.MAX = N + 1
        self.Q = [(None, None)] * self.MAX
        self.head = 0
        self.tail = 0

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

    def enqueue(self, p):
        self.Q[self.tail] = p
        self.tail += 1
        if self.tail == self.MAX:
            self.tail = 0

    def dequeue(self):
        self.head += 1
        if self.head == self.MAX:
            self.head = 0

        return self.Q[self.head - 1]


N, Q = (int(n) for n in input().split())
processes = ProcessQueue(N)
for i in range(N):
    process = input().split()
    processes.enqueue((process[0], int(process[1])))

time = 0
while not processes.isEmpty():
    process = processes.dequeue()
    if process[1] - Q <= 0:
        time += process[1]
        print(process[0], time)
    else:
        time += Q
        processes.enqueue((process[0], process[1] - Q))

Solution (Python):
class ProcessQueue:
    def __init__(self, N):
        self.MAX = N + 1
        self.Q = [(None, None)] * self.MAX
        self.head = 0
        self.tail = 0

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

    def enqueue(self, p):
        self.Q[self.tail] = p
        self.tail += 1
        if self.tail == self.MAX:
            self.tail = 0

    def dequeue(self):
        self.head += 1
        if self.head == self.MAX:
            self.head = 0

        return self.Q[self.head - 1]


N, Q = (int(n) for n in input().split())
processes = ProcessQueue(N)
for i in range(N):
    process = input().split()
    processes.enqueue((process[0], int(process[1])))

time = 0
while not processes.isEmpty():
    process = processes.dequeue()
    if process[1] - Q <= 0:
        time += process[1]
        print(process[0], time)
    else:
        time += Q
        processes.enqueue((process[0], process[1] - Q))

Solution (Python):
class ProcessQueue:
    def __init__(self, N):
        self.MAX = N + 1
        self.Q = [(None, None)] * self.MAX
        self.head = 0
        self.tail = 0

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

    def enqueue(self, p):
        self.Q[self.tail] = p
        self.tail += 1
        if self.tail == self.MAX:
            self.tail = 0

    def dequeue(self):
        self.head += 1
        if self.head == self.MAX:
            self.head = 0

        return self.Q[self.head - 1]


N, Q = (int(n) for n in input().split())
processes = ProcessQueue(N)
for i in range(N):
    process = input().split()
    processes.enqueue((process[0], int(process[1])))

time = 0
while not processes.isEmpty():
    process = processes.dequeue()
    if process[1] - Q <= 0:
        time += process[1]
        print(process[0], time)
    else:
        time += Q
        processes.enqueue((process[0], process[1] - Q))

Solution (Python):
from collections import deque

def main():
    N, Q = [int(i) for i in input().split()]
    Ts = deque([])

    for _ in range(N):
        name, time = [i for i in input().split()]
        Ts.append({
            "name": name,
            "time": int(time)
        })

    cur_t = 0

    while len(Ts):
        task = Ts.popleft()

        
        if task["time"] > Q:
            task["time"] -= Q
            cur_t += Q
            Ts.append(task)
        else:
            cur_t += task["time"]
            print(task["name"], cur_t)


main()

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):
process_num, qms = map(int, input().split())
raw_procs = [input() for i in range(process_num)]


if __name__ == '__main__':
    procs = []
    for row in raw_procs:
        name, time = row.split()
        procs.append({
            "name": name,
            "time": int(time),
        })

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

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):
def fn(x):
    return [x[0], int(x[1])]

N, q = map(int, input().split())
name_time = list(map(fn, [ input().split() for n in range(N) ]))
sec = 0; finish =[];

while True:
    if len(name_time) == 0:
        break
    elif name_time[0][1] > q:
        sec += q
        name_time[0][1] -= q
        name_time.append(name_time.pop(0))
    else:
        sec += name_time[0][1]
        finish.append([name_time.pop(0)[0],sec])
for f in finish:
    print(*f)

Solution (Python):
from collections import deque
 
process_num, qms = map(int ,input().split())
que = deque({})
 
for i in range(process_num):
    name, time = input().split()
    time = int(time)
    que.append({"name":name, "time":time})
 
if __name__ == '__main__':
    total_time = 0
    while len(que)>0:
        atop = que.popleft()
        spend = min(atop["time"], qms)
        atop["time"] -= spend
        total_time += spend
        if(atop["time"] == 0):
            print("{} {}".format(atop["name"], total_time))
        else:
            que.append(atop)