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 collections
calc_time = 0
hoge = collections.deque()
num, q = [int(x) for x in input().split()]
for _ in range(num):
    name, t = input().split()
    hoge.append([name, int(t)])
while hoge:
    name, t = hoge.popleft()
    if t - q > 0:
        hoge.append([name, t-q])
        calc_time += q
    elif t == q:
        calc_time += q
        print ('{} {}'.format(name, calc_time))
    elif t - q < 0:
        calc_time += t
        print ('{} {}'.format(name, calc_time))

Solution (Python):
import sys
data = []
for line in sys.stdin:
	data.append(line.split())

quontum = int(data[0][1])
targetData = data[1:]
answerData = []
totalQuontum = 0

while targetData :
	target = targetData.pop(0)

	target[1] =  int(target[1]) - quontum

	if target[1]<= 0:
		totalQuontum += quontum + int(target[1])
		target[1] = totalQuontum
		answerData.append(target)
	else :
		targetData.append(target)
		totalQuontum += quontum

for answer in answerData:
	print(str(answer[0]) + ' ' + str(answer[1]))

Solution (Python):
import sys
data = []
for line in sys.stdin:
	data.append(line.split())

quontum = int(data[0][1])
targetData = data[1:]
answerData = []
totalQuontum = 0

while targetData :
	target = targetData.pop(0)

	target[1] =  int(target[1]) - quontum

	if target[1]<= 0:
		totalQuontum += quontum + int(target[1])
		target[1] = totalQuontum
		answerData.append(target)
	else :
		targetData.append(target)
		totalQuontum += quontum

for answer in answerData:
	print(str(answer[0]) + ' ' + str(answer[1]))

Solution (Python):
from collections import deque

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

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

Solution (Python):
import sys
import collections
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=collections.deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
n,q=map(int,s[0].split())
d=deque(e.split()for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 v=int(v)
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
_,q=map(int,s[0].split())
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:
  v-=q
  t+=q
  d.append([k,v])
 else:
  t+=v
  print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):



import sys

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

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

elapse = 0
while proc_q:
    process = proc_q.pop(0)  
    p_time = min(quantum, process[1])
    elapse += p_time
    if p_time == process[1]:
        print(process[0], elapse)
    else:
        proc_q.append([process[0], process[1]-quantum])

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
d=deque(list(e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft();v=int(v)
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
d=deque(list(e.split())for e in s[1:])
t=0
while d:
 k,v=d.popleft();v=int(v)
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;print(k,t)

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print(*a,sep='\n')

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print(*a,sep='\n')

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print(*a,sep='\n')

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print(*a,sep='\n')

Solution (Python):
import sys
from collections import deque
s=sys.stdin.readlines()
q=int(s[0].split()[1])
f=lambda x,y:(x,int(y))
d=deque(f(*e.split())for e in s[1:])
t,a=0,[]
while d:
 k,v=d.popleft()
 if v>q:v-=q;t+=q;d.append([k,v])
 else:t+=v;a+=[f'{k} {t}']
print('\n'.join(a))

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

name = []
time = []

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

from collections import deque
time = deque(time)
name = deque(name)

count = 0
time_out = []
name_out = []

while len(time) > 0:
    a = time.popleft()
    b = name.popleft()
    if a > q:
        a -= q
        time.append(a)
        name.append(b)
        count += q
    else:
        count += a
        time_out.append(count)
        name_out.append(b)

for i, j in zip(name_out, time_out):
    print(i, j)

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

name = []
time = []

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

from collections import deque
time = deque(time)
name = deque(name)

count = 0
time_out = []
name_out = []

while len(time) > 0:
    a = time.popleft()
    b = name.popleft()
    if a > q:
        a -= q
        time.append(a)
        name.append(b)
        count += q
    else:
        count += a
        time_out.append(count)
        name_out.append(b)

for i, j in zip(name_out, time_out):
    print(i, j)

Solution (Python):
import sys
from collections import deque
def m():
 s=sys.stdin.readlines()
 q=int(s[0].split()[1])
 f=lambda x,y:(x,int(y))
 d=deque(f(*e.split())for e in s[1:])
 t,a=0,[]
 while d:
  k,v=d.popleft()
  if v>q:v-=q;t+=q;d.append([k,v])
  else:t+=v;a+=[f'{k} {t}']
 print('\n'.join(a))
if'__main__'==__name__:m()

Solution (Python):
import sys
from collections import deque
def m():
 s=sys.stdin.readlines()
 q=int(s[0].split()[1])
 f=lambda x,y:(x,int(y))
 d=deque(f(*e.split())for e in s[1:])
 t,a=0,[]
 while d:
  k,v=d.popleft()
  if v>q:v-=q;t+=q;d.append([k,v])
  else:t+=v;a+=[f'{k} {t}']
 print('\n'.join(a))
if'__main__'==__name__:m()

Solution (Python):
import sys
from collections import deque
def m():
 s=sys.stdin.readlines()
 q=int(s[0].split()[1])
 f=lambda x,y:(x,int(y))
 d=deque(f(*e.split())for e in s[1:])
 t,a=0,[]
 while d:
  k,v=d.popleft()
  if v>q:v-=q;t+=q;d.append([k,v])
  else:t+=v;a+=[f'{k} {t}']
 print('\n'.join(a))
if'__main__'==__name__:m()

Solution (Python):
import sys
from collections import deque
def m():
 s=sys.stdin.readlines()
 q=int(s[0].split()[1])
 f=lambda x,y:(x,int(y))
 d=deque(f(*e.split())for e in s[1:])
 t,a=0,[]
 while d:
  k,v=d.popleft()
  if v>q:v-=q;t+=q;d.append([k,v])
  else:t+=v;a+=[f'{k} {t}']
 print('\n'.join(a))
if'__main__'==__name__:m()

Solution (Python):
n, q = map(int, input().split(' '))
L = []
i = 0
while i < n:
    name, time = map(str, input().split(' '))
    L.append([name, int(time)])
    i += 1
t = 0
fin = []
while len(L) != 0:
    if L[0][1] > q:
        t += q
        name = L[0][0]
        time = L[0][1] - q
        L.append([name, time])
        L.pop(0)
    else:
        t += L[0][1]
        name = L[0][0]
        time = t
        fin.append([name, time])
        L.pop(0)
ans = ''
for f in fin:
    ans += f[0] + ' ' + str(f[1]) + '\n'
print(ans[:-1])

Solution (Python):
import queue
q = queue.Queue()
exetime = 0

n, quantum = map(int, input().split())
for i in range(n):
    q.put(tuple(input().split())) 

while not q.empty():
    process = q.get()
    
    time = int(process[1])
    if time > quantum:
        exetime += quantum
        q.put((process[0], time - quantum))
    else:
        exetime += time
        print(process[0], exetime)

Solution (Python):
n,q = map(int,input().split())
A = []
B = []
C = []
t = 0

for i in range(n):
    x,y = input().split()
    A.append([x,int(y)])

while len(A)>0:
    f = A[0][1]
    if f<=q:
        t += f
        B.append(A[0][0])
        C.append(t)
    else:
        t += q
        A[0][1] -= q
        A.append(A[0])
    A.pop(0)

for j in range(len(B)):
    print(B[j],C[j])

Solution (Python):
import sys

import copy

fline = input().rstrip().split(' ')
N, Q = int(fline[0]), int(fline[1])

list = []
for i in range(N):
    tlist = []
    line = input().split(' ')
    tlist = [line[0], line[1]]
    
    list.append(tlist)


current = 0
while len(list) != 0:
    if int(list[0][1]) <= Q:
        current += int(list[0][1])
        print(list[0][0] + " " + str(current))
        del list[0]
    else:
        list[0][1] = str((int)(list[0][1])-Q)
        list.append(list[0])
        del list[0]
        current += Q

Solution (Python):
n, q = map(int, input().split())
l = []
for i in range(n):
  t = []
  for j in input().split():
    t += [j]
  t[1] = int(t[1])
  l += [t]

c = 0
while len(l) > 0:
  if l[0][1] > q:
    l[0][1] -= q
    l += [l[0]]
    l.pop(0)
    c += q
  else:
    c += l[0][1]
    print(l[0][0], c)
    l.pop(0)

Solution (Python):



from collections import deque

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

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

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


n, quantum = map(int, input().split())
queue = []
for i in range(n):
    queue.append(parse(input()))
head = 0


def enqueue(x):
    global queue
    queue.append(x)


def dequeue():
    global head
    head += 1


total_time, count = 0, 0
while count < n:
    y = queue[head][1] - quantum
    if y <= 0:
        print(str(queue[head][0]) + " " + str(total_time + queue[head][1]))
        dequeue()
        total_time += y
        count += 1
    else:
        queue[head][1] -= quantum
        enqueue(queue[head])
        dequeue()

    total_time += quantum

Solution (Python):
from collections import deque


def run_process(processes, slot):
    """Simulate the round-robin scheduler.
    Returns a list of (process, elapsed_time).

    >>> ps = [('p1', 150), ('p2', 80), ('p3', 200), ('p4', 350), ('p5', 20)]
    >>> run_process(ps, 100)
    [('p2', 180), ('p5', 400), ('p1', 450), ('p3', 550), ('p4', 800)]
    """
    ps = deque(processes)
    tot = 0
    finish = []

    while len(ps) > 0:
        p, t = ps.popleft()
        dt = min(t, slot)
        if dt == t:
            finish.append((p, tot+dt))
        else:
            ps.append((p, t-dt))

        tot += dt

    return finish


def run():
    count, slot = [int(i) for i in input().split()]
    ps = []

    for _ in range(count):
        p, t = input().split()
        ps.append((p, int(t)))

    for p, t in run_process(ps, slot):
        print(p, t)


if __name__ == '__main__':
    run()

Solution (Python):
n, q = [int(e) for e in input().split()]
A = []
res = []
for i in range(n):
    l = input().split()
    A.append((l[0], int(l[1])))

idx = 0
tot_t = 0
while A:
    dif = A[0][1] - q
    if dif <= 0:
        tot_t += A[0][1]
        res.append((A[0][0], tot_t))
        A.pop(0)
    else:
        tmp = (A[0][0], dif)
        A.pop(0)
        A.append(tmp)
        tot_t += q

for e in res:
    print(e[0], e[1])

Solution (Python):
n, q = [int(a) for a in input().split()]
total_time = 0
queue = []
for i in range(n):
    queue.append([a for a in input().split()])

while len(queue) > 0:
    name = queue[0][0]
    time = int(queue[0][1])
    if time <= q:
        total_time += time
        print(name + " " + str(total_time))
        del queue[0]
    else:
        total_time += q
        time -= q
        queue.append([name, time])
        del queue[0]

Solution (Python):
n,q = list(map(int, input().split()))
p = [0]*n
t = [0]*n

for i in range(n):
    p[i],t[i] = list(input().split())

i = 0
sum_t = 0
while 1:
    if i==len(t):
        break
    tmp_t = int(t[i])-q
    if tmp_t<=0:
        sum_t += int(t[i])
        print(p[i],sum_t)
    else:
        sum_t += q
        p.append(p[i])
        t.append(tmp_t)        
    i+=1

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

loop=0
while loop==0:
    for i in range(len(Q)//2):
        tmp=[Q[0],Q[1]]
        if tmp[1]>q:
            sum+=q
            Q.append(tmp[0])
            Q.append(tmp[1]-q)
        else:
            sum+=tmp[1]
            print(tmp[0],sum)
        Q.pop(0)
        Q.pop(0)
    if len(Q)==0:
        break

Solution (Python):
n, q = map(int, input().split())
l = []
for i in range(n):
  t = []
  for j in input().split():
    t += [j]
  t[1] = int(t[1])
  l += [t]
 
c = 0
while len(l) > 0:
  if l[0][1] > q:
    l[0][1] -= q
    l += [l[0]]
    l.pop(0)
    c += q
  else:
    c += l[0][1]
    print(l[0][0], c)
    l.pop(0)

Solution (Python):
n, q = map(int,input().split())
l = []
for i in range(n):
  t=[]
  for j in input().split():
    t+=[j]
  t[1]=int(t[1])
  l+=[t]
 
c=0
while len(l)>0:
  if l[0][1]>q:
    l[0][1]-=q
    l+=[l[0]]
    l.pop(0)
    c+=q
  else:
    c+=l[0][1]
    print(l[0][0],c)
    l.pop(0)

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



from collections import deque

n, q = [int(x) for x in input().split(' ')]

queue = deque([])
for i in range(n):
    inp = input().split(' ')
    queue.append([inp[0], int(inp[1])])

elapsed_time = 0
while True:
    proc = queue.popleft()
    if proc[1] - q <= 0:
        elapsed_time += proc[1]
        print(proc[0], elapsed_time)
    else:
        elapsed_time += q
        queue.append([proc[0], proc[1] - q])
    if len(queue) == 0:
        break

Solution (Python):
n, p = map(int, input().split())
ps = []
for i in range(n):
    ps.append(input().split())
    ps[i][1] = int(ps[i][1])
t = 0
while len(ps) > 0:
    if ps[0][1] > p:
        ps.append([ps[0][0], ps[0][1] - p])
        t += p
        ps.pop(0)
    else:
        t += ps[0][1]
        print("{0} {1}".format(ps[0][0], t))
        ps.pop(0)

Solution (Python):

"""
Created on Sun Apr 29 14:42:43 2018

@author: maezawa
"""

def print_array(g):
    ans = str(g[0])
    if len(g) > 1:
        for i in range(1,len(g)):
            ans += ' '+str(g[i]) 
    print(ans)

name=[]
time=[]
current_time = 0

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





finished = []
fin_time = []
i=0
remain = n
while True:
    if time[i] <= q:
        current_time += time.pop(i)
        finished.append(name.pop(i))
        fin_time.append(current_time)      
        remain -= 1
        if remain == 0:
            break
        i = i%remain
    else:
        time[i] -= q
        current_time += q
        i = (i+1)%remain

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))

Solution (Python):

"""
Created on Sun Apr 29 18:04:22 2018
ALDS1_3b_r リングバッファによる実装
@author: maezawa
"""

def fifo_enque(data):
    global tail
    global fifo
    fifo[tail] = data
    tail = (tail+1)%fifo_size
    
def fifo_deque():
    global head
    global fifo
    data = fifo[head]
    head = (head+1)%fifo_size
    return data

fifo_size = 100000
fifo = [0 for _ in range(fifo_size)]
head = 0
tail = 0

n, q = list(map(int, input().split()))
for i in range(n):
    s = input().split()
    data = [s[0], int(s[1])]
    fifo_enque(data)
    
current_time = 0
finished = []
fin_time = []

while True:
    data = fifo_deque()
    if data[1] > q:
        current_time += q
        data[1] -= q
        fifo_enque(data)
    else:
        current_time += data[1]
        finished.append(data[0])
        fin_time.append(current_time)
    if head == tail:
        break

for i in range(n):
    print("{} {}".format(finished[i], fin_time[i]))

Solution (Python):
a=input().split()
a=[int(i) for i in a]
b=[]
cnt=0
time=0
for i in range(a[0]):
  b.append(input().split())
  cnt+=int(b[i][1])
i=0
while cnt!=time:
  if int(b[i][1])<=a[1]:
    time+=int(b[i][1])
    print(b[i][0],time)
    b.pop(0)
  else: 
    b[i][1]=int(b[i][1])-a[1]
    time+=a[1]
    b.append(b.pop(i))

Solution (Python):
s=input()
d=s.split()
n=int(d[0])
q=int(d[1])
end_t=0
processes=[]
for i in range(n):
    processes.append([])
    s=input()
    d=s.split()
    processes[i].append(d[0])
    processes[i].append(int(d[1]))

cnt=0
i=0
while True:
    if processes[0][1]<=q:
       end_t+=processes[0][1]
       print(processes[0][0],end_t)
       processes.pop(0)
       cnt+=1
       if cnt==n:
           break
   
    else:
       processes[0][1]-=q
       end_t+=q
       processes.append(processes[0])
       processes.pop(0)
       i+=1

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

N = [] 
T1 = [] 


for i in range(n):
    a = input().split()
    N.append(a[0])
    T1.append(int(a[1]))

t = 0 
f = 0 
l = len(N)-1 

while len(N) > 0:
    if T1[f] <= q: 
        t += T1[f]
        if f == len(N)-1:
            print(N.pop(f), t)
            del T1[f]
            f = 0
        elif f == 0:
            print(N.pop(f), t)
            del T1[f]
            l -= 1
        else:
            print(N.pop(f), t)
            del T1[f]
        
        if len(N) == 0: break

    else: 
        t += q
        T1[f] -= q

        i = N[f]
        j = T1[f]

        if f+1 == len(N): f = 0
        else: f += 1
        if l+1 == len(N): l = 0
        else: l += 1
        
        N[l] = i
        T1[l] = j

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

t = 0
n_end = []
t_end = []
while(len(time) > 0):
    if time[0] <= q:
        t += time[0]
        n_end.append(name.pop(0))
        time.pop(0)
        t_end.append(t)
    else :
        t += q
        time[0] -= q
        a = time.pop(0)
        b = name.pop(0)
        time.append(a)
        name.append(b)

for i in range(len(n_end)):
    print(n_end[i],t_end[i])

Solution (Python):


from collections import deque

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

t=0

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