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())
name = []
time = []
for i in range(n):
    tmp = input().split()
    name.append(str(tmp[0]))
    time.append(int(tmp[1]))
    
total = 0
result = []
while True:
    try:
        if time[0] > q:
            total += q
            time[0] = time[0] - q
            tmp = time.pop(0)
            time.append(tmp)
            tmp = name.pop(0)
            name.append(tmp)
        elif q >= time[0] and time[0] > 0:
            total += time[0]
            time.pop(0)
            a = name.pop(0)
            print(a,total)
        else:
            break
    except:
        break

Solution (Python):
n, q = map(int, input().split())
data = []
for i in range(n):
    tmp = input().split()
    tmp[1] = int(tmp[1])
    data.append(tmp)

dt_out = []
time_count = 0

while len(data) > 0 :
    tmp = data.pop(0)
    if(tmp[1] <= q):
        time_count += tmp[1]
        dt_out.append([tmp[0],time_count])
    else:
        tmp[1] -= q
        time_count += q
        data.append(tmp)

for tmp in dt_out:
    print(tmp[0]+" "+str(tmp[1]))

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

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

Solution (Python):
from collections import deque

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

for i in range(n):
    a = input().split()
    Q.append([a[0],int(a[1])])
s = 0

while Q:
    qt = Q.popleft()
    
    if qt[1] > q:
        Q.append([qt[0],qt[1]-q])
        s += q
    else:
        s += qt[1] 
        print(qt[0],s)

Solution (Python):
def robin(process, q):
    if int(process[0][1]) - q > 0:
        process[0][1] = int(process[0][1]) - q
        process.append([process[0][0],process[0][1]])
        process.pop(0)
        return process, q
    else:
        q = int(process[0][1])
        process.pop(0)
        return process, q


n, q = input().split()
n, q = int(n), int(q)
process = []
for i in range(n):
    process.append([i for i in input().split()])


time = 0
while len(process) != 0:
    length = len(process)
    name = process[0][0]
    process, process_time = robin(process, q)
    time += process_time
    if length > len(process):
        print(name,time)

Solution (Python):
def initialize():
    global head, tail


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


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


def enqueue(x):
    global head, tail, Q, MAX
    if isFull():
        print("?????????????????????")
    else:
        Q[tail] = x
        if tail + 1 == MAX:
            tail = 0
        else:
            tail += 1


def dequeue():
    global head, tail, Q, MAX
    if isEmpty():
        print("??¢??????????????????")
    else:
        t = Q[head]
        if head + 1 == MAX:
            head = 0
        else:
            head += 1
        return t


if __name__ == "__main__":
    n, q = map(int, input().split())
    head = 0
    tail = n
    MAX = 100000
    Q = [0]*MAX
    runtime = 0
    for i in range(n):
        name, time = input().split()
        Q[i] = ([name, int(time)])
    while head != tail:
        t_list = dequeue()
        if t_list[1] > q:
            t_list[1] -= q
            runtime += q
            enqueue(t_list)
        else:
            runtime += t_list[1]
            t_list[1] = 0
            print(t_list[0] + " " + str(runtime))

Solution (Python):
from collections import deque

if __name__ == '__main__':
    n, q = map(int, input().split())
    Q = deque()
    for i in range(n):
        name,time = input().split()
        Q.append([name, int(time)])
    runtime = 0
    while Q:
        qt = Q.popleft()
        if qt[1] > q:
            runtime += q
            qt[1] -= q
            Q.append(qt)
        else:
            runtime += qt[1]
            print(qt[0],runtime)

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

s = list()
process = 0
t = list()
for i in range(n):
    name, time = map(str, input().split())
    s.append([name, int(time)])

flag = True
while s:
    diff = s[0][1] - q
    if diff <= 0:
        process += s[0][1]
        t.append([s[0][0], process])
        s.pop(0)
    else:
        process += q
        s.append([s[0][0], diff])
        s.pop(0)
for i in t:
    print(i[0], i[1])

Solution (Python):
class Node:
    elm = None
    nxt = None

    def __init__(self, elm, nxt):
        self.elm = elm
        self.nxt = nxt


class MyQueue:
    head = tail = None
    size = 0

    def enqueue(self, x):
        if self.is_empty():
            self.head = self.tail = Node(x, None)
        else:
            self.tail.nxt = Node(x, None)
            self.tail = self.tail.nxt
        self.size += 1

    def dequeue(self):
        n = self.head
        if not self.is_empty():
            self.head = self.head.nxt
            self.size -= 1
        return n.elm

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

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

s = MyQueue()
result = list()
process = 0

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

while not s.is_empty():

    e = s.dequeue()
    diff = e[1] - q
    if diff <= 0:
        process += e[1]
        result.append([e[0], process])
    else:
        process += q
        s.enqueue([e[0], diff])
for i in result:
    print(i[0], i[1])

Solution (Python):
n, q = map(int, input().split())
l = [list(input().split()) for i in range(n)]
from collections import deque
que = deque([[l[i][0], int(l[i][1])] for i in range(n)])
a = []
time = 0
while que != deque([]):
    a = que.popleft()
    if a[1] <= q:
        time += a[1]
        print("{} {}".format(a[0], str(time)))
    else:
        time += q
        a[0], a[1] = a[0], a[1] - q
        que.append(a)

Solution (Python):
from collections import deque

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

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

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

i = 0
totalT = 0
numElements = n
while True:
    if lt[i] <= q:
        totalT += lt[i]
        dispName = ln.pop(i)
        lt.pop(i)
        print(dispName, str(totalT), sep = ' ')
        numElements -= 1
        if numElements <= 0:
            break
        i = i%numElements
    else:
        totalT += q
        lt[i] -= q
        i = (i + 1)%numElements

Solution (Python):
import collections
import sys

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

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

p = []
time = 0

for i in range(n):
	p.append(input().split())
	p[i][1] = int(p[i][1])

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

Solution (Python):

class CPU():
    def __init__(self, in_quantum):
        self.quantum = in_quantum
        self.total_time = 0
        
    def deal(self, in_process, in_time):
        sum_process_time = 0
        this_process_time = 0

        sum_process_time += in_time[0]
        
        if sum_process_time > self.quantum:
            remain_time = sum_process_time - self.quantum
            this_process_time = in_time[0] - remain_time
            sum_process_time = self.quantum
        
            in_process.append(in_process[0])
            in_time.append(remain_time)
            
            self.total_time += this_process_time
        else:
            this_process_time = in_time[0]
            self.total_time += this_process_time
            
            self.makeLog(in_process[0])

        in_process.pop(0)
        in_time.pop(0)
        
        return in_process, in_time

    def makeLog(self, in_process):
        print(in_process, self.total_time)

    
processes = []
times = []
    
data_cnt, quantum =  list(map(int,input().split(" ")))  

for i in range(data_cnt):
    tmp_process, tmp_time = input().split(" ")
    processes.append(tmp_process)
    times.append(int(tmp_time))

objCPU = CPU(quantum)

while True:
    processes, times = objCPU.deal(processes, times)

    if len(processes) == 0:
        break

Solution (Python):


import sys
from collections import deque

class CPU():
    def __init__(self, in_quantum):
        self.quantum = in_quantum
        self.total_time = 0
        
    def deal(self, in_process, in_time):
        this_process_time = 0

        if in_time[0] > self.quantum:
            remain_time = in_time[0] - self.quantum
            this_process_time = in_time[0] - remain_time

            in_process.append(in_process[0])
            in_time.append(remain_time)
            
            self.total_time += this_process_time
        else:
            this_process_time = in_time[0]
            self.total_time += this_process_time
            
            self.makeLog(in_process[0])

        in_process.popleft()
        in_time.popleft()
        
        return in_process, in_time

    def makeLog(self, in_process):
        print(in_process, self.total_time)

    
processes = deque()
times = deque()
    
data_cnt, quantum =  list(map(int,input().split(" ")))  

for i in range(data_cnt):
    tmp_process, tmp_time = input().split(" ")
    processes.append(tmp_process)
    times.append(int(tmp_time))

objCPU = CPU(quantum)

while True:
    processes, times = objCPU.deal(processes, times)
    if len(processes) == 0:
        break

Solution (Python):



n, quantum = map(int, raw_input().split())

process_time = []
tim = 0

for x in xrange(n):
    pros, tt = raw_input().split()
    process_time.append([pros, int(tt)])

while True:
    if len(process_time) == 0: break
    pp = process_time.pop(0)
    if pp[1] > quantum :
        pp[1] = pp[1] - quantum
        process_time.append(pp)
        tim += quantum
    else:
        tim += pp[1]
        print pp[0] + " " +str(tim)

Solution (Python):
n, q = map(int, input().split())
que = []
for _ in range(n):
    p, t = input().split()
    que.append([p, int(t)])

now = 0
while len(que) != 0:
    if que[0][1] > q:
        que[0][1] -= q
        now += q
        que.append(que.pop(0))
    else:
        now += que[0][1]
        print("{} {}".format(que[0][0], now))
        que.pop(0)

Solution (Python):


import sys
import os
from queue import Queue


N, Q = list(map(int, input().split()))

q = Queue()
for i in range(N):
    lst = input().split()
    name = lst[0]
    t = int(lst[1])
    data = [name, t]
    q.put(data)

time = 0
while not q.empty():
    data = q.get()
    if data[1] > Q:
        data[1] -= Q
        q.put(data)
        time += Q
    else:
        time += data[1]
        print(data[0], time)

Solution (Python):
n, q = [int(temp) for temp in input().split()]
tim  = 0
dat  = []

for mak in range(n) :
    tem = [temp for temp in input().split()]
    tem[1] = int(tem[1])
    if tem[1] > q :
        tim += q
        dat.append(tem[0])
        dat.append(tem[1] - q)
    else :
        tim += tem[1]
        print(tem[0], tim)

while dat != [] :
    for mak in range(len(dat) // 2) :
        tem = [dat[0], dat[1]]
        if tem[1] > q :
            tim += q
            dat.append(tem[0])
            dat.append(tem[1] - q)
        else :
            tim += tem[1]
            print(tem[0], tim)
        dat.pop(0)
        dat.pop(0)

Solution (Python):


import sys
from collections import deque

input_ = sys.stdin.readlines()


def message_queue():
    
    n, shard = map(int, input_[0].split())

    task_deque = deque(map(lambda x: x.split(), input_[1:]))
    t_accu = 0

    while task_deque:
        t_name, t_time = task_deque.popleft()
        t_time = int(t_time)
        if t_time <= shard:
            t_accu += t_time
            print(t_name, t_accu)
        else:
            t_time -= shard
            t_accu += shard
            task_deque.append([t_name, t_time])


message_queue()

Solution (Python):
from collections import deque

n, q = map(int, input().split())
name = deque()
time = deque()
for i in range(n):
    tmp_name, tmp_time = input().split()
    name.append(tmp_name)
    time.append(int(tmp_time))

ans_time = 0

while len(time)>0:
    tmp_time = time.popleft()
    tmp_name = name.popleft()
    if tmp_time<=q:
        ans_time += tmp_time
        print(tmp_name + " " + str(ans_time))
    else:
        ans_time += q
        time.append(tmp_time-q)
        name.append(tmp_name)

Solution (Python):
s = raw_input().split()
n = int(s[0])
q = int(s[1])

process_name = []
process_time = []
for x in xrange(n):
  ps = raw_input().split()
  process_name.append(ps[0])
  process_time.append(int(ps[1]))

total_time = 0
while process_name != []:
  pn = process_name.pop(0)
  pt = process_time.pop(0)
  if pt <= q:
    total_time += pt
    print pn, total_time
  else: 
    total_time += q
    process_name.append(pn)
    process_time.append(pt-q)

Solution (Python):
import queue
n, q = map(int, input().split(" "))
plist = queue.Queue()
for i in range(n):
    plist.put((lambda l: [l[0], int(l[1])])(input().split(" ")))
time = 0
history = queue.Queue()
while not plist.empty():
    p = plist.get()
    if p[1] <= q:
        time += p[1]
        history.put([p[0], time])
    else:
        p[1] -= q
        plist.put(p)
        time += q
while not history.empty():
    p = history.get()
    print("%s %d" % (p[0], p[1]))

Solution (Python):
a = list(map(int, input().split()))
queuekey = []
queuetime = []

for i in range(0, a[0]):
    tmp = list(input().split())
    queuekey.append(tmp[0])
    queuetime.append(int(tmp[1]))

spentTime = 0
while len(queuetime) > 0:
    if queuetime[0] <= a[1]:
        spentTime += queuetime[0]
        print(queuekey[0], end="")
        print(' ', end="")
        print(spentTime)
        queuekey.pop(0)
        queuetime.pop(0)
    else:
        spentTime += a[1]
        queuetime[0] -= a[1]
        queuetime.append(queuetime[0])
        queuekey.append(queuekey[0])
        queuetime.pop(0)
        queuekey.pop(0)

Solution (Python):
from collections import deque
n, q = map(int,raw_input().split())
queue = deque([])
for i in range(n):
    queue.append(map(str,raw_input().split()))

queue = deque(map(lambda x : [x[0],int(x[1])], queue)) 
end  = deque([])
time = 0

while len(queue) > 0:
    xqt = queue.popleft()
    res_time = xqt[1] - q
    if res_time <= 0:
        time = time + xqt[1]
        end.append([xqt[0],time])
    else:
        time = time + q
        xqt[1] = res_time
        queue.append(xqt)

while len(end) > 0:
    out = end.popleft()
    print out[0], out[1]

Solution (Python):
from collections import deque
n, q = map(int,raw_input().split())
queue = deque([])
for i in range(n):
    queue.append(map(str,raw_input().split()))

queue = deque(map(lambda x : [x[0],int(x[1])], queue)) 

time = 0

while len(queue) > 0:
    xqt = queue.popleft()
    res_time = xqt[1] - q
    if res_time <= 0:
        time = time + xqt[1]
        print xqt[0], time
    else:
        time = time + q
        xqt[1] = res_time
        queue.append(xqt)

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

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

time=0
while len(list)>0:
    if list[0][1]<=q:
        time+=list[0][1]
        print(list[0][0]+" "+str(time))
        list.pop(0)
    else:
        time+=q
        list[0][1]-=q
        list.append(list[0])
        list.pop(0)

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

Solution (Python):


import collections

p,q=map(int,input().split())
count =0

deq = collections.deque()
finished=[]

while 1:
    try:
        deq.append(input().split())
        
    except EOFError:
        break

while deq:
    d = deq.popleft()
    d[1]=int(d[1])
    if d[1]<=q:
        count+=d[1]
        d[1]=count
        finished.append(map(str,d))
    elif d[1]>q:
        d[1]-=q
        count+=q
        deq.append(d)
for f in finished:
    print(" ".join(f))

Solution (Python):
from collections import deque

def round_robin_scheduling(n, q, A):
	sum = 0

	while A:
		head = A.popleft()
		if head[1] <= q:
			sum += head[1]
			print(head[0], sum)
		else:
			sum += q
			head[1] -= q
			A.append(head)

if __name__ == '__main__':
	n, q = map(int, input().split())
	A = deque()

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

Solution (Python):
sch = []
fin = []
total = 0
cnt,time = map(int,input().split())

for i in range(cnt):
    n,t = input().split()
    sch.append([n,int(t)])

while len(sch) > 0:
    if sch[0][1] <= time:
        total += sch[0][1]
        v = sch.pop(0)
        fin.append([v[0],total])
    else:
        total += time
        sch[0][1] -= time
        last = sch.pop(0)
        sch.append(last)

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

Solution (Python):
n,q = map(int, input().split())
MAX = n + 2
Q = [0 for i in range(MAX)]
head = 0
tail = 0

def enqueue(u, t):
    global tail
    Q[tail] = [u, t]
    tail = (tail + 1) % MAX

def dequeue():
    global head
    x,t = Q[head]
    head = (head + 1) %MAX
    return [x,t]
for i in range(n):
    P = list(input().split())
    enqueue(P[0], int(P[1]))

elaps = 0
while head != tail:
    u,t = dequeue()
    c = min(q, t)
    t -= c
    elaps += c
    if t > 0:
        enqueue(u,t)
    else:
        print(u + " " + str(elaps))

Solution (Python):

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

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):


n, q = map(int, input().split())
queue = []
total_time = 0
finished_loop = 0

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

while True:
    n -= finished_loop
    finished_loop = 0
    for i in range(n):
        poped = queue.pop(0)
        name = poped[0]
        time = poped[1]
        if time > q:
            queue.append([name, time - q])
            total_time += q
        else:
            total_time += time
            print(name, total_time)
            finished_loop += 1

    if n == 0:
        break

Solution (Python):

import sys

n, q = map(int, sys.stdin.readline().split())
queue = []
total_time = 0
finished_loop = 0

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

while queue:
    n -= finished_loop
    finished_loop = 0
    for i in range(n):
        poped = queue.pop(0)
        name = poped[0]
        time = poped[1]
        if time > q:
            queue.append([name, time - q])
            total_time += q
        else:
            total_time += time
            print(name, total_time)
            finished_loop += 1

Solution (Python):


n, q = map(int, input().split())
queue = []
total_time = 0
finished_loop = 0

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

while queue:
    n -= finished_loop
    finished_loop = 0
    for i in range(n):
        poped = queue.pop(0)
        name = poped[0]
        time = poped[1]
        if time > q:
            queue.append([name, time - q])
            total_time += q
        else:
            total_time += time
            print(name, total_time)
            finished_loop += 1

Solution (Python):

from collections import deque

n, q = map(int, input().split())
queue = deque()
total_time = 0

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

while queue:
    poped = queue.popleft()
    if poped[1] > q:
        queue.append([poped[0], poped[1] - q])
        total_time += q
    else:
        total_time += poped[1]
        print(poped[0], total_time)

Solution (Python):

from collections import deque

n, q = map(int, input().split())
queue = deque(maxlen = 100000)
total_time = 0

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

while queue:
    poped = queue.popleft()
    if poped[1] > q:
        queue.append([poped[0], poped[1] - q])
        total_time += q
    else:
        total_time += poped[1]
        print(poped[0], total_time)

Solution (Python):
s = input().split(" ")
n = int(s[0])
q = int(s[1])

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

queue = []

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

def dequeue():
    global head
    x = queue[head]
    head = (head + 1) % n
    return x
    
def enqueue(x):
    global tail
    tail = (tail + 1) % n
    queue[tail] = x

time = 0

while head != tail:
    w = dequeue()
    if w.time > q:
        w.time = w.time - q
        time = time + q
        enqueue(w)
    else:
        time = time + w.time
        print("{0} {1}".format(w.name, time))
print("{0} {1}".format(queue[head].name, time+queue[head].time))

Solution (Python):

"""
Created on Tue Jun 20 20:19:33 2017

@author: syaga
"""

if __name__ == "__main__":
    n, q = map(int, input().split())
    a = []
    for i in range(n):
        b = input().split()
        b[1] = int(b[1])
        a.append(b)
    ans = []
    time = 0
    while len(a) != 0:
        x = a.pop(0)
        if x[1] <= q:
            time += x[1]
            ans.append([x[0], time])
        else:
            x[1] -= q
            time += q
            a.append(x)
    for i in ans:
        print(" ".join(map(str, i)))

Solution (Python):
import collections as cols

n, q = [int(x) for x in input().split()]
qu = cols.deque()

for i in range(n):
	tmp = [x for x in input().split()]
	qu.append([tmp[0], int(tmp[1])])

num = n
t = 0

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

Solution (Python):
if __name__ == "__main__":
      S = (input()).split()
      n = int(S[0])
      q = int(S[1])
      head = 0
      tail = n-1
      que = []
      runtime = 0
      fin = []

      for i in range(0, n):
            que.append((input()).split())

      while head <= tail:
            nowjob = que[head]
            jobtime = int(nowjob[1])
            if jobtime > q :
                  jobtime = jobtime -q
                  jobname = nowjob[0]
                  que.append([jobname, jobtime])
                  runtime = runtime + q
                  head = head + 1
                  tail = tail + 1
            else :
                  runtime = runtime + jobtime
                  fin.append([(que[head])[0], runtime])
                  head = head + 1
      for i in range(0,n):
            print("{} {}".format((fin[i])[0], (fin[i])[1]))

Solution (Python):
import Queue
import sys

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

def main():
    array = map(lambda x: int(x), sys.stdin.readline().strip().split(" "))
    p_num = array[0]
    q_time = array[1]
    p_queue = Queue.Queue()
    for i in range(0, p_num):
        array = sys.stdin.readline().strip().split(" ")
        p_queue.put(Proc(array[0], int(array[1])))
    t = 0
    while not p_queue.empty():
        p = p_queue.get()
        if p.time > q_time:
            p.time = p.time - q_time
            p_queue.put(p)
            t += q_time
        else:
            t += p.time
            print p.name + " " + str(t)
    
if __name__ == "__main__":
    main()

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

Solution (Python):


"""
?????\???
?????? namei ??¨????????????????????? timei ????????? n ???????????????????????????????????????????????§????????????
??????????????????????????±?????\???????????°??¨?????°????????????????????§??????CPU ?????????????????????????????????????????????
???????????????????????§ q ms?????????????????????????????¨???????????????????????????????????????????????????
q ms ????????????????????£????????????????????????????????????????????????????????°???
??????????????????????????????????°?????§??????????CPU ????¬??????????????????????????????????????????????

??????????????????????????±?????\???????????°???????????\??¬????????????????????°????????????????????????????????????
"""


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

total = 0
while len(QUE) > 0:
    p = QUE.pop(0)
    pt = p[1] - q

    if pt < 1:
        total += p[1]
        CNP.append([p[0],total])

    else:
        p[1] = pt
        QUE.append(p)

        total += q
for i in CNP:
    print("{} {}".format(i[0],i[1]))

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

time = 0
while queue:
	processing = queue.pop(0)
	t = min(processing[1], q)
	time += t
	processing[1] -= t
	if processing[1] == 0:
		print(processing[0],time)
	else:
		queue.append(processing)

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

n_t=[input().split() for i in range(n)]

p_time=0

while n_t:
    
    if int(n_t[0][1])<=q:
        p_time+=int(n_t[0][1])
        finish=n_t.pop(0)
        print(finish[0],p_time)
    else:
        
        n_t[0][1]=str(int(n_t[0][1])-q)
        n_t.append(n_t[0])
        p_time+=q
        del n_t[0]

Solution (Python):
from queue import Queue
n,q = map(int,input().split())
qe = Queue()
current = 0
for i in range(n):
    name, time = input().split()
    time = int(time)
    qe.put((name, time))

while not qe.empty():
    item = qe.get()
    if item is None:
        break
    name, time = item
    c = min(q, time)
    current += c
    time -= c
    if time > 0:
        qe.put((name, time))
    else:
        print(name + ' ' + str(current))

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

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

Solution (Python):
class Queue():
    def __init__(self):
        self.el = []
        
    def add(self, el):
        self.el.append(el)
        
    def delete(self):
        return self.el.pop(0)
    
    def action(self):
        self.el[0]['time'] -= q
        tmp = self.delete()
        self.el.append(tmp)

n, q = map(int, raw_input().split())
qu = Queue()

for i in range(n):
    name, time = raw_input().split()
    time = int(time)
    qu.add({'name':name, 'time':time})

time = 0
while qu.el:
    
    if qu.el[0]['time'] > q:
        qu.action()
        time += q
    else:
        time += qu.el[0]['time']
        print qu.el[0]['name'], time
        qu.delete()

Solution (Python):
from collections import deque

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

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

cnt = 0

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

Solution (Python):

from collections import deque

N, time = [int(n) for n in input().split()]

dq = deque()

for n in range(N):
    inp = input().split()
    dq.append([inp[0], int(inp[1])])

now = 0    
while len(dq):
    name, task = dq.popleft()
    if task > time:
        task -= time
        now += time
        dq.append([name, task])
    else:
        now += task
        print("{} {}".format(name, now))