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

Solution (Python):

import sys
import collections

def main():
	n, quantum = map(int, raw_input().split())
	processes = [x.split() for x in sys.stdin.readlines()]
	for p in processes:
		p[1] = int(p[1])
	queue = collections.deque(processes)
	elapsed = 0
	while queue:
		
		head = queue.popleft()
		if head[1] > quantum:
			head[1] -= quantum
			queue.append(head)
			elapsed += quantum
		else:
			elapsed += head[1]
			print head[0], elapsed

if __name__ == '__main__':
	main()

Solution (Python):
import collections
n, q = map(int, input().split())
P = collections.deque()
for _ in range(n):
    name, time = input().split()
    P.append([name, int(time)])

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

Solution (Python):
from collections import deque

n, qt = map(int, input().split())
que = deque()
for i in range(n):
    name, time = input().split()
    que.append((name, int(time)))

elapsedtime = 0
while que:
    name, time = que.popleft()
    if time <= qt:
        elapsedtime += time
        print(name, elapsedtime)
    else:
        elapsedtime += qt
        time -= qt
        que.append((name, time))

Solution (Python):
from Queue import Queue
n, q = map(int, raw_input().split())
P = []
for i in xrange(n):
    x = raw_input().split()
    P.append((x[0], int(x[1])))

Q = Queue()
for x in P:
    Q.put(x)
total = 0
while not Q.empty():
    x = Q.get()
    if x[1] <= q:
        total += x[1]
        print x[0], total
    else:
        total += q
        Q.put((x[0], x[1]-q))

Solution (Python):


import sys


def main():
    lines = sys.stdin.readlines()
    (_, quantum) = lines[0].split(' ')
    quantum = int(quantum)

    processes = []
    for line in lines[1:]:
        if line.strip() == '':
            break

        (k, v) = line.strip().split(' ')
        processes.append((k, int(v)))

    time = 0
    result = []
    while True:
        (process, process_time) = processes.pop(0)  

        if process_time <= quantum:
            time += process_time
            result.append((process, time))
        else:
            time += quantum
            processes.append((process, process_time - quantum))

        if len(processes) == 0:
            break

    for item in result:
        print("%s %s" % (item[0], item[1]))

if __name__ == "__main__":
    main()

Solution (Python):
N_str, q_str = raw_input().split()
N = int(N_str)
q = int(q_str)

A = []
B = []

for i in range(N):
    a = raw_input().split()
    A.append([a[0], int(a[1])])

total_time = 0
while A:
    name, time = A.pop(0)
    if time <= q:
        total_time += time 
        B.append("%s %d" % (name, total_time))
    else:
        time -= q
        A.append([name, time])
        total_time += q
print '\n'.join(B)

Solution (Python):
N_str, q_str = raw_input().split()
N = int(N_str)
q = int(q_str)

A = []
B = []

for i in range(N):
    a = raw_input().split()
    A.append([a[0], int(a[1])])

total_time = 0
while A:
    name, time = A.pop(0)
    if time <= q:
        total_time += time 
        B.append("%s %d" % (name, total_time))
    else:
        time -= q
        A.append([name, time])
        total_time += q
print '\n'.join(B)

Solution (Python):
N_str, q_str = raw_input().split()
N = int(N_str)
q = int(q_str)
A =[]
B =[]
total =0

for i in range(N):
    t = raw_input().split()
    A.append([t[0],int(t[1])])

while A:
    name, time = A.pop(0)
    if time <= q:
        total += time
        B.append(name+" "+ str(total))
       
    else:
        time -= q
        total += q
        A.append([name, time])

print '\n'.join(B)

Solution (Python):
N_str, q_str = raw_input().split()
N = int(N_str)
q = int (q_str)
A =[]
B =[]
total =0

for i in range(N):
    t = raw_input().split()
    A.append([t[0],int(t[1])])

while A:
    name, time = A.pop(0)
    if time <= q:
        total += time
        B.append(name+" "+ str(total))
       
    else:
        time -= q
        total += q
        A.append([name, time])

print '\n'.join(B)

Solution (Python):
import sys

queue = []

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

for line in sys.stdin.readlines():
  (name, time) = line.strip().split()
  queue.append((name, int(time)))

total_time = 0
while len(queue) > 0:
  (name, time) = queue.pop(0)
  time -= q
  if time > 0:
    total_time += q
    queue.append((name, time))
  else:
    total_time += time + q
    print "%s %s" % (name, total_time)

Solution (Python):
a = raw_input()
n, q = a.split(" ")
n = int(n)
q = int(q)
total = 0

queue = []
for i in range(n):
	queue.append(raw_input())
while (len(queue) > 0):
	b = queue.pop(0)
	p, t = b.split(" ")
	sa = int(t) - q
	if sa > 0:
		queue.append(p + " " + str(sa))
		total += q
	else:
		total += int(t)
		print (p + " " + str(total))

Solution (Python):
import sys

def isEmpty(S):
  if len(S) == 0:
    return True

def isFull(S):
  if len(S) >= 100000:
    return True

def enqueue(x):
  if isFull(x_list_list):
    print 'Error'
  x_list_list.append(x)

def dequeue(x_list_list):
  if isEmpty(x_list_list):
    print 'Error'
  del_list = x_list_list[0]
  x_list_list.pop(0)
  return(del_list)

y = sys.stdin.readline()
a, b = y.split(" ")
a = int(a)
b = int(b)

S = []
x_list_list = []
run_time = 0

for i in range(a):
  text = raw_input()
  x_list = text.split()
  x_list[1] = int(x_list[1])
  x_list.append(0)
  x_list_list.append(x_list)

flag = a
while flag != 0:
  if x_list_list[0][1] > b:
    x_list_list[0][1] -= int(b)
    run_time += b
    del_list = dequeue(x_list_list)
    enqueue(del_list)
  else:
    run_time += x_list_list[0][1]
    x_list_list[0][2] = run_time
    x_list_list[0][1] = 0
    flag -= 1
    print x_list_list[0][0],x_list_list[0][2]
    dequeue(x_list_list)

Solution (Python):
class Trio:
    __pre=None
    __val=None
    __nex=None
    def __init__(self,val,p=None,n=None):
        self.__pre=p
        self.__val=val
        self.__nex=n
    
    def get_pre(self):return self.__pre
    
    def get_nex(self):return self.__nex
    
    def set_pre(self,n):self.__pre=n
        
    def set_nex(self,n):self.__nex=n
    
    def set_val(self,n):self.__val=n
    
    def get_val(self):return self.__val
    
class Queue:
    top = back = None
    def __init__(self):
        pass
    
    def enqueue(self,n):
        if(self.top==None):
            self.top = self.back = Trio(n)
        else:
            self.back.set_nex(Trio(n,p=self.back))
            self.back=self.back.get_nex()
    
    def dequeue(self):
        if(self.top==None):
            return None
        elif(self.top==self.back):
            a=self.top.get_val()
            self.top,self.back=None,None
            return a
        else:
            self.top=self.top.get_nex()
            oldtop=self.top.get_pre()
            self.top.set_pre(None)
            return oldtop.get_val()

s = input()
n,limit=map(int,s.split())
que=Queue()
for i in range(n):
    lis=input().split()
    que.enqueue((lis[0],int(lis[1])))

time=0
while(True):
    elm=que.dequeue()
    if(elm==None):
        break
    elif(elm[1]>limit):
        que.enqueue((elm[0],elm[1]-limit))
        time+=limit
    else:
        time+=elm[1]
        print(elm[0],time)

Solution (Python):


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

while queue:
    ps = queue.pop(0)
    time = int(ps[1])

    if time <= q:
        total_time += time
        print(ps[0], total_time)
    else:
        total_time += q
        time -= q
        ps[1] = time
        queue.append(ps)

Solution (Python):


from collections import deque

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

while q:
    ps_name, ps_time = q.popleft()
    if ps_time <= quantum:
        total_time += ps_time
        print(ps_name, total_time)
    else:
        total_time += quantum
        ps_time -= quantum
        q.append([ps_name, ps_time])

Solution (Python):
class Queue():
    def __init__(self):
        self.size = 0
        self.Max = 100000
        self.queue = [None]

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

    def isFull(self):
        return self.size >= self.Max

    def enqueue(self, x):
        if self.isFull():
            print("Queue overflow!")
        else:
            self.queue.append(x)
            self.size += 1

    def dequeue(self):
        if self.isEmpty():
            print("Queue underflow!")
        else:
            self.size -= 1
            return self.queue.pop(1)

p_queue = Queue()
e_time = 0

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

for i in range(n):
    process = input().split()
    process[1] = int(process[1])
    p_queue.enqueue(process)

while not p_queue.isEmpty():
    process = p_queue.dequeue()
    if process[1] <= q:
        e_time += process[1]
        print(process[0], e_time)
    else:
        e_time += q
        process[1] -= q
        p_queue.enqueue(process)

Solution (Python):
class Queue():
    def __init__(self):
        self.size = 0
        self.Max = 100000
        self.queue = [None]

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

    def isFull(self):
        return self.size >= self.Max

    def enqueue(self, x):
        if self.isFull():
            print("Queue overflow!")
        else:
            self.queue.append(x)
            self.size += 1

    def dequeue(self):
        if self.isEmpty():
            print("Queue underflow!")
        else:
            self.size -= 1
            return self.queue.pop(1)

p_queue = Queue()
e_time = 0
ans = ""

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

for i in range(n):
    process = input().split()
    process[1] = int(process[1])
    p_queue.enqueue(process)

while not p_queue.isEmpty():
    process = p_queue.dequeue()
    if process[1] <= q:
        e_time += process[1]
        ans += "{0} {1}\n".format(process[0], e_time)
    else:
        e_time += q
        process[1] -= q
        p_queue.enqueue(process)

print(ans, end = "")

Solution (Python):
class Queue():
    def __init__(self):
        self.size = 0
        self.Max = 100000
        self.queue = [None]

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

    def isFull(self):
        return self.size >= self.Max

    def enqueue(self, x):
        if self.isFull():
            print("Queue overflow!")
        else:
            self.queue.append(x)
            self.size += 1

    def dequeue(self):
        if self.isEmpty():
            print("Queue underflow!")
        else:
            self.size -= 1
            return self.queue.pop(1)

p_queue = Queue()
e_time = 0

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

for i in range(n):
    process = input().split()
    process[1] = int(process[1])
    p_queue.enqueue(process)

while not p_queue.isEmpty():
    process = p_queue.dequeue()
    if process[1] <= q:
        e_time += process[1]
        print(process[0], e_time)
    else:
        e_time += q
        process[1] -= q
        p_queue.enqueue(process)

Solution (Python):
class Process_Queue:
    class Process:
        def __init__(self, name, r_time, link = None):
            self.name = name
            self.r_time = r_time
            self.link = link

    def __init__(self, q):
        self.size = 0
        self.e_time = 0
        self.q = q
        self.front = None
        self.rear  = None

    def enqueue(self, name, r_time):
        if self.size == 0:
            self.front = self.rear = Process_Queue.Process(name, r_time)
        elif self.size == 100000:
            raise Exception("queue overflow")
        else:
            new_process = Process_Queue.Process(name, r_time)
            self.rear.link = new_process
            self.rear = new_process
        self.size += 1

    def dequeue(self):
        if self.size == 0:
            raise Exception("queue underflow")
        elif self.front.r_time > self.q:
            self.enqueue(self.front.name, self.front.r_time - self.q)
            self.front = self.front.link
            self.e_time += self.q
        elif self.front.r_time <= self.q:
            p_name = self.front.name
            self.e_time += self.front.r_time
            print(p_name, self.e_time)
            self.front = self.front.link
        self.size -= 1
        if self.size == 0:
            self.rear = None
                
    def isEmpty(self):
        return self.size == 0
    
    def isFull(self):
        return self.size == 100000


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

p_queue = Process_Queue(q)

for i in range(n):
    p = input().split()
    p_queue.enqueue(p[0], int(p[1]))

while not p_queue.isEmpty():
    p_queue.dequeue()

Solution (Python):
class Process_Queue:
    class Process:
        def __init__(self, name, r_time, link = None):
            self.name = name
            self.r_time = r_time
            self.link = link

    def __init__(self, q):
        self.e_time = 0
        self.q = q
        self.front = None
        self.rear  = None

    def enqueue(self, name, r_time):
        if self.front == self.rear == None:
            self.front = self.rear = Process_Queue.Process(name, r_time)
        else:
            new_process = Process_Queue.Process(name, r_time)
            self.rear.link = new_process
            self.rear = new_process

    def dequeue(self):
        if self.front == self.rear == None:
            raise Exception("queue underflow")
        elif self.front.r_time > self.q:
            self.enqueue(self.front.name, self.front.r_time - self.q)
            self.front = self.front.link
            self.e_time += self.q
        else:
            p_name = self.front.name
            self.e_time += self.front.r_time
            print(p_name, self.e_time)
            self.front = self.front.link
        if self.front == None:
            self.rear = None

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

p_queue = Process_Queue(q)

for i in range(n):
    p = input().split()
    p_queue.enqueue(p[0], int(p[1]))

while not p_queue.front == p_queue.rear == None:
    p_queue.dequeue()

Solution (Python):
import collections
import sys

p_q = collections.deque(maxlen = 100000)

e_time = 0

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

for i in range(n):
    p_name, r_time = sys.stdin.readline().split()
    p_q.append([p_name, int(r_time)])

while p_q:
    t_p = p_q.popleft()
    if t_p[1] > q:
        t_p[1] -= q
        e_time += q
        p_q.append(t_p)
    else:
        e_time += t_p[1]
        print(t_p[0], e_time)

Solution (Python):



import collections


class Process(object):

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


def schedule(processes, quantum):
    queue = collections.deque(processes)
    time = 0
    while len(queue) > 0:
        p = queue.popleft()
        rest_time = p.time - quantum
        if rest_time > 0:
            time += quantum
            queue.append(Process(p.name, rest_time))
        else:
            time += p.time
            print("{name} {time:d}".format(name=p.name, time=time))


def main():
    [n, q] = list(map(int, input().split()))
    processes = [Process(*input().split()) for i in range(n)]
    schedule(processes, q)


if __name__ == "__main__":
    main()

Solution (Python):


n, q = map(int, raw_input().split())
queue = []
for i in xrange(n):
    name, time = raw_input().split()
    queue.append((name, int(time)))

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

Solution (Python):
class Queue:
    def __init__(self,values):
        self.values = values

    def empty(self):
        if len(self.values) == 0:
            return True
        else:
            return False

    def enqueue(self,v):
        self.values.append(v)

    def dequeue(self):
        if len(self.values) <= 0:
            raise
        else:
            v = self.values[0]
            del self.values[0]
            return v

n,q = map(int,raw_input().split(' '))
processes = []
for i in range(n):
    n,t = raw_input().split(' ')
    processes.append((n,int(t)))

queue = Queue(processes)

clock = 0
done = []
while not queue.empty():
    p = queue.dequeue()
    n = p[0]
    t = p[1]
    if t <= q:
        clock+=t
        done.append((n,clock))
    else:
        queue.enqueue((n,t-q))
        clock+=q

for p in done:
    print p[0],p[1]

Solution (Python):
from collections import deque

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

queue = deque()

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

total_time = 0

while queue:
    q_t = queue.popleft()
    if q_t[1] <= qt:
        total_time += q_t[1]
        print(q_t[0], total_time)
    else:
        total_time += qt
        q_t[1] -= qt
        queue.append(q_t)

Solution (Python):
import collections

dq = collections.deque()
N, Q = map(int, input().split())
for i in range(N):
    name, time = input().split()
    dq.append((name, int(time)))
total_time = 0
while dq:
    name, time = dq.popleft()
    total_time += min(Q, time)
    if time <= Q:
        print(name, total_time)
    else:
        dq.append((name, time-Q))

Solution (Python):

from collections import deque
n, quantum = [int(i) for i in input().split()]
dq = deque()
for i in range(n):
    p, time = [str(i) for i in input().split()]
    dq.append([p, int(time)])
time = 0
while(len(dq) != 0):
    
    
    if dq[0][1] > quantum:
        dq[0][1] -= quantum
        dq.rotate(-1)
        time += quantum
    
    else:
        time += dq[0][1]
        print("{} {}".format(dq[0][0], time))
        dq.popleft()

Solution (Python):
def parse(l):
    name, time = l.split()
    return (name, int(time))


n, q = map(int, input().split())
queue = list(parse(input()) for _ in range(n))
time = 0

while queue:
    (name, remain_time) = queue.pop(0)
    if remain_time > q:
        time += q
        queue.append((name, remain_time - q))
    else:
        time += remain_time
        print(name, time)

Solution (Python):
nq = str(input()).split()
n = int(nq[0])
q = int(nq[1])
p = [str(input()).split() for i in range(n)]
p = [(str(i[0]), int(i[1])) for i in p]
ans = []
time = 0
while len(p) != 0:
    t = p.pop(0)
    if t[1] > q:
        
        time += q
        p.append((t[0], t[1]-q))
    elif t[1] <= q:
        
        ans.append((t[0], t[1]+time))
        time += t[1]
for t in ans:
    print('{0} {1}'.format(t[0], t[1]))

Solution (Python):
n, q = map(int, input().split())
queue = list()
for i in range(n):
    p = input().split()
    queue.append({"name": p[0], "time": int(p[1])})

t = 0
while len(queue) > 0:
    proc = queue.pop(0)
    if q < proc["time"]:
        proc["time"] -= q
        t += q
        queue.append(proc)
    else:
        t += proc["time"]
        print(proc["name"], t)

Solution (Python):
from collections import deque
import sys

def main():
    n, q = map(int,raw_input().split())
    Q = deque([])
    for i in xrange(n):
        a = raw_input().split()
        a[1] = int(a[1])
        Q.append(a)
    
    elaps = 0
    while len(Q) > 0:
        u = Q.popleft()
        a = min(u[1], q)
        u[1] -= a
        elaps += a
        if u[1] > 0:
            Q.append(u)
        else:
            print u[0], elaps
    return 0

main()

Solution (Python):
n, q = (int(x) for x in 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):
class Queue:
    def __init__(self):
        self.data = []

    def enqueue(self, data):
        self.data.append(data)

    def dequeue(self):
        res = self.data.pop(0)
        return res

    def getLength(self):
        return len(self.data)

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

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

while processes.getLength() > 0:
    currentProcess = processes.dequeue()
    processName = currentProcess[0]
    processCost = currentProcess[1]

    if processCost <= q:
        time += processCost
        print(' '.join([processName, str(time)]))
        continue

    time += q
    processes.enqueue([processName, processCost - q])

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

def queue(q, ps):
    time = 0
    while len(ps) != 0:
        p = ps.pop(0)
        if p.time > q:
            p.time -= q
            time += q
            ps.append(p)
        else:
            time += p.time
            print p.name + " " + str(time)

if __name__ == "__main__":
    L = map(int, raw_input().split())
    N = L[0]
    q = L[1]
    processes = []
    for i in range(N):
        L = raw_input().split()
        processes.append(process(L[0], int(L[1])))
    queue(q, processes)

Solution (Python):
n, q = map(int, raw_input().split())
A = [[0, 0] for i in range(n)]

for i in range(n):
    temp = raw_input().split()
    A[i][0] = temp[0]
    A[i][1] = int(temp[1])

time = 0
while A != []:
    if A[0][1] - q > 0:
        A[0][1] = A[0][1] - q
        time = time + q
        A.append(A.pop(0))
    else:
        print A[0][0],
        print time + A[0][1]
        time = time + A[0][1]
        A.pop(0)

Solution (Python):
n,q=map(int,raw_input().split())
p=[map(str,raw_input().split()) for i in xrange(n)]
a=[]
ct=0
while len(p)>=1:
    if int(p[0][1])>q:
        p.append([p[0][0],int(p[0][1])-q])
        p.pop(0)
        ct+=q
        
    else:
        ct+=int(p[0][1])
        a.append([p[0][0],int(ct)])
        p.pop(0)
for i in xrange(n):
    print a[i][0],a[i][1]

Solution (Python):
N_str, q_str = raw_input().split()
N = int(N_str)
q = int(q_str)

A = []
B = []

for i in range(N):
    a = raw_input().split()
    A.append([a[0], int(a[1])])

total_time = 0
while A:
    name, time = A.pop(0)
    if time <= q:
        total_time += time
        B.append("%s %d" % (name, total_time))
    else:
        time -= q
        A.append([name, time])
        total_time += q
print '\n'.join(B)

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

	sum = 0

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

Solution (Python):
from collections import deque


n, q = map(int, input().split())
n_t = deque()
for i in range(n):
    s, j = input().split()
    n_t.append([str(s), int(j)])

timer = 0

while n_t:
    s, i = n_t.popleft()
    if i > q:
        i -= q
        timer += q
        n_t.append([s, i])
    else:
        timer += i
        print("{0} {1}".format(s, timer))

Solution (Python):


import collections

n, q = map(int, raw_input().split())
prolist = [raw_input().split() for i in range(n)]
prolist = collections.deque(prolist)
time_sum = 0
while len(prolist):
    process = prolist.popleft()
    time = int(process[1])
    if time <= q:
        time_sum += time
        print "%s %d" %(process[0], time_sum)
    else:
        time_sum += q
        process[1] = str(time-q)
        prolist.append(process)

Solution (Python):
from collections import deque

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

Solution (Python):
class Queue(object):

    def __init__(self, l):
        self._queue = l
        self._n = len(l)
        self._head = 0
        self._tail = 0

    def pop_left(self):
        if self._head == self._n:
            self._head = 0
        ret = self._queue[self._head][::]
        self._head += 1

        return ret

    def push_right(self, x):
        if self._tail == self._n:
            self._tail = 0
        self._queue[self._tail] = x
        self._tail += 1

if __name__ == '__main__':
    n, q = [int(x) for x in raw_input().split()]
    ps_list = []
    for _ in range(n):
        raw = raw_input().split()
        ps_list.append([raw[0], int(raw[1])])

    queue = Queue(ps_list)

    n_end_process = 0
    acc_time = 0
    while n_end_process < n:
        h = queue.pop_left()
        if h[1] <= q:
            n_end_process += 1
            acc_time += h[1]
            print "{} {}".format(h[0], acc_time)
        else:
            h[1] -= q
            acc_time += q
            queue.push_right(h)

Solution (Python):
n, time=map(int,raw_input().split())
p = []
t = []
for _ in xrange(n):
    a = raw_input().split()
    p.append(a[0])
    t.append(int(a[1]))
alltime = 0
while True:
    if len(p)==0:
        break
    if t[0]<=time:
        alltime+=t[0]
        print p.pop(0),
        t.pop(0)
        print alltime
    else:
        t.append(t[0])
        t.pop(0)
        t[-1]-=time
        alltime += time
        p.append(p[0])
        p.pop(0)

Solution (Python):

n, q = map(int, raw_input().split())
procs = [raw_input().split() for i in xrange(n)]
procs = list(reversed(procs))


cnt, lack = 0, 1
while lack > 0:
    lack = 0
    tprocs = []
    for i in xrange(n):
        proc = procs.pop()
        t = int(proc[1])
        if t <= q:
            cnt += t
            print proc[0], cnt
            n -= 1
        else: 
            cnt += q
            t = t - q
            lack += t
            proc[1] = str(t)
            tprocs.append(proc)
    procs = list(reversed(tprocs))

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

Solution (Python):
from collections import deque
n,q = list(map(int, input().split()))
P = deque([[p,int(t)] for p,t in [input().split() for _ in range(n)]])
time = 0
while len(P) > 0:
    pi = P.popleft()
    if pi[1] > q:
        P.append([pi[0],pi[1]-q])
        time += q
    else:
        time += pi[1]
        print(pi[0],time)

Solution (Python):



def round_robin_scheduling(process_name, process_time, q):
    result = []
    elapsed_time = 0

    while len(process_name) > 0:
        
        
        if process_time[0] <= q:
            elapsed_time += process_time[0]
            process_time.pop(0)
            result.append(process_name.pop(0) + ' ' + str(elapsed_time))
        else:
            elapsed_time += q
            process_name.append(process_name.pop(0))
            process_time.append(process_time.pop(0) - q)
    return result


if __name__ == '__main__':
    N, q = map(int, raw_input().split())
    process_name, process_time = [], []
    for i in xrange(N):
        process = raw_input().split()
        process_name.append(process[0])
        process_time.append(int(process[1]))
    
    
    
    result = round_robin_scheduling(process_name, process_time, q)
    for i in xrange(len(result)):
        print result[i]

Solution (Python):

sample_input = list(range(3))
sample_input[0] = '''5 100
p1 150
p2 80
p3 200
p4 350
p5 20'''
sample_input[1] = ''''''
sample_input[2] = ''''''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        

import copy

class Queue:
    queue = []
    def __init__(self, queue_list = []):
        self.queue = copy.copy(queue_list)
        pass
    def dequeue(self):
        if self.queue == []:
            return None
        return self.queue.pop(0)
        pass
    def enqueue(self, item):
        self.queue.append(item)

class Process:
    name = None
    time = -1
    def __init__(self, a_name, a_time):
        self.name = a_name
        self.time = a_time
    def execute(self, a_time):
        used_time = min(a_time, self.time)
        self.time -= a_time
        return used_time
    def is_ended(self):
        if self.time <= 0:
            return True
        else:
            return False
            
input_str = input()
num_of_process = int(input_str.split()[0])
quantum = int(input_str.split()[1])

input_process = []

for n in range(num_of_process):
    input_str = input()
    input_split = input_str.split()
    process = Process(input_split[0], int(input_split[1]))
    input_process.append(process)

process_queue = Queue(input_process)

time = 0
while True:
    process = process_queue.dequeue()
    if process is None:
        break
    used_time = process.execute(quantum)
    time += used_time
    if process.is_ended():
        timestamp = process.name + ' ' + str(time)
        print(timestamp)
    else:
        process_queue.enqueue(process)

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

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

for i in range(0, n):
    name, time = input().split(' ')
    time = int(time)
    procs.append(Proc(name, time))

elapsed = 0
while len(procs) > 0:
    proc = procs.pop(0)
    if proc.time > q:
        proc.time -= q
        elapsed += q
        procs.append(proc)
    else:
        elapsed += proc.time
        print(proc.name, elapsed)