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):
from collections import deque

class Task:
    def __init__(self, name, cost):
        self.name = name
        self.cost = cost

class RoundRobin:
    def __init__(self):
        self.time = 0
        self.queue = deque()
        self.num_of_task, self.quantum = map(int, input().split())
        
    def enqueue(self, task):
        self.queue.append(task)
    
    def dequeue(self):
        return self.queue.popleft()


rr = RoundRobin()

for _ in range(rr.num_of_task):
    input_task = input().split()
    rr.enqueue(Task(input_task[0], int(input_task[1])))

while len(rr.queue) > 0:
    task = rr.dequeue()
    if task.cost <= rr.quantum:
        rr.time += task.cost
        print("%s %d"%(task.name, rr.time))
    else:
        task.cost -= rr.quantum
        rr.time += rr.quantum
        rr.enqueue(task)

Solution (Python):
from collections import deque

def queue(n, t):
    q = deque([])
    for i in range(n):
        process, time = input().split()
        time = int(time)
        q.append([process, time])

    finish = 0  

    while len(q) > 0:
        top = q.popleft()  
        s = min(top[1], t)  
        top[1] -= s
        finish += s
        if top[1] == 0:
            print(top[0], finish)   
        else:
            q.append(top)   

if "__main__" == __name__:
    n, t = map(int, input().split())
    queue(n, t)

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

answer = []
time = 0
while len(proc) != 0:
    target = proc.pop(0)

    if target[1] <= q:
        time += target[1]
        answer.append([target[0], time])
    else:
        time += q
        target[1] -= q
        proc.append(target)

[print(*answer[i]) for i in range(len(answer))]

Solution (Python):
from collections import deque

class process():
    def __init__(self, name, ptime):
        self.name = name
        self.ptime = ptime

def main():
    n, q = map(int, input().split())
    w_queue = deque()

    for _ in range(n):
        name, ptime = input().split()
        p = process(name, int(ptime))
        w_queue.append(p)

    t = 0

    while True:
        p = w_queue.popleft()
        
        if p.ptime <= q:
            print(p.name, str(t + p.ptime))
            t += p.ptime
        else:
            p.ptime -= q
            w_queue.append(p)
            t += q
       
        if len(w_queue) == 0:
            break
        
main()

Solution (Python):
from queue import Queue


def main():
    n, q = map(int, input().split(' '))
    process_time = Queue()
    process_name = Queue()
    for _ in range(n):
        name, time = input().split(' ')
        process_name.put(name)
        process_time.put(int(time))
    cnt_time = 0
    while not (process_time.empty()):
        time = process_time.get()
        if time <= q:
            cnt_time += time
            print(process_name.get(), cnt_time)
        else:
            cnt_time += q
            process_time.put(time - q)
            process_name.put(process_name.get())


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

ps = deque()
for i in range(n):
  p = [ i for i in input().split(" ") ]
  ps.append([p[0], int(p[1])])

time = 0
while len(ps) > 0:
  p = ps.popleft()

  tmp = p[1] - q
  if tmp > 0:
    ps.append([p[0], tmp])
    time +=  q
  else:
    time += p[1]
    print(p[0], str(time))

Solution (Python):
import queue


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

    def __repr__(self):
        return '{} {}'.format(self.name, self.value)


Q = queue.Queue()

n, q = map(int, input().split())
for _ in range(n):
    name, value = input().split()
    Q.put(Process(name, int(value)))

t = 0
while not Q.empty():
    process = Q.get()
    if process.value > q:
        process.value -= q
        Q.put(process)
        t += q
    else:
        t += process.value
        print('{} {}'.format(process.name, t))

Solution (Python):

input_number,input_time = map(int, input().split())
input_data = list()
for i in range(input_number):
    input_data.append(list(input().split()))
    input_data[i][-1] = int(input_data[i][-1])


temp_list = list()
total_time = 0
result_list = list()
while len(input_data) > 0:
    temp_list = input_data[0]
    if temp_list[-1] > input_time:
        total_time += input_time
        temp_list[-1] -= input_time
        input_data.append(temp_list)
    else:
        total_time += temp_list[-1]
        temp_list[-1] = total_time
        result_list.append(temp_list)
    del input_data[0]


[print(" ".join(map(str, i))) for i in result_list]

Solution (Python):
n,m=map(int,input().split())
p=[input().split() for _ in range(n)]
cnt=0
while(p):
    if int(p[0][1])>m:
        p[0][1]=int(p[0][1])-m
        x=p[0]
        del(p[0])
        p.append(x)
        cnt+=m
    else:
        cnt+=int(p[0][1])
        print(p[0][0], cnt)
        del(p[0])

Solution (Python):
def show(a):
    print(a[0], a[1], sep = ' ')


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

list_a = []
for i in range(n):
    a = input().split()
    list_a.append(a) 

time = 0          

while len(list_a) != 0:
    if int(list_a[0][1]) > q:
        time += q 
        list_a[0][1] = (int(list_a[0][1]) - q)
        a = list_a.pop(0)
        list_a.append(a)
    else :
        time += int(list_a[0][1])
        list_a[0][1] = time
        a = list_a.pop(0)
        show(a)

Solution (Python):
from collections import deque
n, q = list(map(int, input().split()))

que = deque([])
for _ in range(n):
    que.append((int(x) if x.isdigit() else x for x in input().split()))
    
lap = 0

while que:
    name, lefttime = que.popleft()
    if lefttime <= q:
        lap += lefttime
        print(name, lap)
    else:
        lefttime -= q
        lap += q
        que.append((name, lefttime))

Solution (Python):
from collections import deque
n, q = map(int, input().split())
d = deque()
for i in range(n):
    tmp = input().split()
    tmp[1] = int(tmp[1])
    d.append(tmp)
time = 0
while len(d) > 0:
    tmp = d.popleft()
    time += min(q, tmp[1])
    if tmp[1] > q:
        tmp[1] -= q
        d.append(tmp)
    else:
        print(tmp[0] + ' ' + str(time))

Solution (Python):



class Process:
    name = ""
    time = 0

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


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

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

time_in_now = 0
while queue:
    if queue[0].time > q:
        queue[0].time -= q
        time_in_now += q
        temp = queue[0]
        queue.pop(0)
        queue.append(temp)
    else:
        time_in_now += queue[0].time
        print(f"{queue[0].name} {time_in_now}")
        queue.pop(0)

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

Solution (Python):
class Queue():
    def __init__(self):
        self.queue = [None] * 100000
        self.head = 0
        self.tail = 0
        self.max_queue = len(self.queue)

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

    def is_full(self):
        return self.head == (self.tail + 1) % self.max_queue

    def enqueue(self, x):
        if self.is_full():
            raise ValueError("Error: Full!")

        self.queue[self.tail] = x

        if self.tail + 1 == self.max_queue:
            self.tail = 0
        else:
            self.tail += 1

    def dequeue(self):
        if self.is_empty():
            raise ValueError("Error: Empty!")

        x = self.queue[self.head]

        if self.head + 1 == self.max_queue:
            self.head = 0
        else:
            self.head += 1
        return x


process_name = Queue()
process_cost = Queue()

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

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

    process_name.enqueue(name)
    process_cost.enqueue(time)

at_time = 0
while not process_name.is_empty():
    name = process_name.dequeue()
    cost = process_cost.dequeue()
    if cost > q:
        process_name.enqueue(name)
        process_cost.enqueue(cost - q)
        at_time += q
    else:
        at_time += cost
        print(f"{name} {at_time}")
        pass

    pass

Solution (Python):
from collections import deque

n, q = map(int, input().split())
que = deque()
for _ in range(n):
	for x in input().split():
		if (x.isdigit()):
			que.append(int(x))
		else:
			que.append(x)
t = 0
while que:
	name = que.popleft()
	lefttime = que.popleft()
	if (lefttime <= q):
		t += lefttime
		print(name, t)
	else:
		t += q
		lefttime -= q
		que.append(name)
		que.append(lefttime)

Solution (Python):
import collections
d = collections.deque([])
n, p = map(int, input().split())
for i in range(n):
    name, t = input().split()
    d.append((name, int(t)))
tt = 0
while len(d) > 0:
    name, t = d.popleft()
    if t <= p:
        tt += t
        print("{0} {1}".format(name, tt))
    elif t > p:
        t = t - p
        tt += p
        d.append((name, t))

Solution (Python):




import collections


def main():
    n, q = map(int, input().split())
    deq = collections.deque()
    for i in range(n):
        name, time = input().split()
        deq.append((int(time),name))
    ans = []
    elps = 0
    while deq:
        time, name = deq.popleft()
        if time <= q:
            elps += time
            ans.append((name, elps))
        else:
            elps += q
            deq.append((time-q, name))
    [print(*i) for i in ans]
    


    
if __name__ == '__main__':
    main()

Solution (Python):
import sys


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

    def isEmpty(self):
        return len(self.Q) == 0

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

    def dequeue(self):
        if self.isEmpty():
            sys.exit("Empty")
        else:
            return self.Q.pop(0)



n, power = map(int, raw_input().split(" "))
process_list = [map(str, raw_input().split(" ")) for i in range(n)]
process_list = [(p[0], int(p[1])) for p in process_list]

q = Queue()
for p in process_list:
    q.enqueue(p)
elapsed_time = 0
while not q.isEmpty():
    process = q.dequeue()
    if process[1] > power:
        elapsed_time += power
        q.enqueue((process[0], process[1] - power))
    else:
        elapsed_time += process[1]
        print(str(process[0]) + " " + str(elapsed_time))

Solution (Python):
from collections import deque

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

que = deque()

for _ in range(n):
	s, t = input().split()
	t = int(t)
	que.append((s, t))

t = 0
while len(que)>0:
	name, time = que.popleft()
	if time<=q:
		t += time
		print("{} {}".format(name,t))
	else:
		que.append((name, time-q))
		t += q

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

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

class Queue:
  def __init__(self, length):
    self.q = [Process('p0', 0)]*length
    self.MAX = length
    self.head = 0
    self.tail = 0
  
  def enqueue(self, x):
    self.q[self.tail] = x
    if self.tail+1 == self.MAX:
      self.tail = 0
    else:
      self.tail += 1 
  
  def dequeue(self):
    x = self.q[self.head]
    if self.head + 1 == self.MAX:
      self.head = 0
    else: 
      self.head += 1
    return x
  
  def isEmpty(self):
    if self.head == self.tail:
      return True
    else:
      return False

  def isFull(self):
    if self.head == (self.tail+1)%self.MAX:
      return True
    else:
      False


Q = Queue(100000) 

for _ in range(n):
  x,y = map(str, input().split())
  tmp = Q.enqueue(Process(x,int(y)))

time = 0
while not Q.isEmpty():
  qtop = Q.dequeue()
  if qtop.time > qtm:
    time += qtm
    qtop.time -= qtm
    Q.enqueue(qtop) 
  else:
    time += qtop.time
    qtop.time = 0
    print(qtop.name + ' ' + str(time))

Solution (Python):
import collections


num_proc, quantum = tuple(map(int, input().split(' ')))
proc_list = collections.deque()

for i in range(0, num_proc):
    proc_elem = input().split(' ')
    proc_elem[1] = int(proc_elem[1])
    proc_list.append(proc_elem)

timer = 0
while bool(proc_list):
    if proc_list[0][1] <= quantum:
        timer += proc_list[0][1]
        print(f'{proc_list.popleft()[0]} {timer}')
    else:
        timer += quantum
        proc_list[0][1] -= quantum
        proc_list.append(proc_list.popleft())

Solution (Python):
queue = []

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

def dequeue():
  v = queue[0]
  del queue[0]

  return v

n, q = map(int, input().split())
for i in range(n):
  p, t = input().split()
  queue.append({"p": p, "t": int(t)})

sum_t = 0
while len(queue) > 0:
  v = dequeue()
  c = min(v["t"], q)
  sum_t += c
  v["t"] -= c
  if v["t"] > 0:
    enqueue(v)
  else:
    print (v["p"], sum_t)

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

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

Solution (Python):
global Q
global N
def deque():
    global head
    x=Q[1][head]
    if head+1==maxn:
        head=0
    else:
        head=head+1
    return x
def enque(i,a):
    global tail
    Q[0][tail]=Q[0][i]
    Q[1][tail]=a
    if tail+1==maxn:
        tail=0
    else:
        tail=tail+1
n,q=map(int,input().split())
Q=[[0 for j in range(n)] for i in range(2)]
N=[[0 for j in range(n)] for i in range(2)]
head=0
tail=0
for i in range(0,n):
    a,b=input().split()
    Q[0][i]=a
    Q[1][i]=(int(b))
maxn=n
i=0
k=0
time=0
while 1:
    if Q[1][i]>q:
        time=time+q
        l=deque()-q
        enque(i,l)
        i=i+1
        if i==maxn:
            i=0
    else:
        time=time+deque()
        N[0][k]=Q[0][i]
        N[1][k]=time
        i=i+1
        if tail==head and k==n-1:
            time=time+Q[1][head]
            break
        if i==maxn:
            i=0
        k=k+1
for i in range(0,n):
    print(N[0][i],end=" ")
    print(N[1][i])

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

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

Solution (Python):
from collections import deque

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):
class Queue:
    def __init__(self):
        self.queue = []
    def push(self, x):
        self.queue.append(x)
    def pop(self):
        return self.queue.pop(0)
    def isEmpty(self):
        return len(self.queue) == 0

n, q = list(map(int, input().split(' ')))
process_queue = Queue()
for i in range(n):
    name, time = list(input().split(' '))
    process_queue.push([name, int(time)])

total_time = 0
while True:
    name, time = process_queue.pop()
    if time <= q:
        print('{} {}'.format(name, time + total_time))
        add_time = time
    else:
        time -= q
        process_queue.push([name, time])
        add_time = q
    total_time += add_time
    if process_queue.isEmpty():
        break

Solution (Python):
from collections import deque
robin=deque()

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

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

time=0
while(len(robin)!=0):
    proc=robin.popleft()
    if(proc[1]<=q):
        print(proc[0],time+proc[1])
        time+=proc[1]
    else:
        proc[1]-=q
        robin.append(proc)
        time+=q

Solution (Python):
from collections import deque

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

    total_time = 0

    dname = deque()
    dtime = deque()

    for i in range(n):
        nametmp, timetmp = input().split()
        dname.append(nametmp)
        dtime.append(int(timetmp))
    
    while dname or dtime: 
        name = dname.popleft()
        time = dtime.popleft()
        if time <= q: 
            total_time += time
            print(name, total_time)
        else: 
            total_time += q
            time -= q
            dname.append(name)
            dtime.append(time)

if __name__ == "__main__":
    main()

Solution (Python):
import queue

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

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

q = queue.Queue()

for _ in range(N):
    name, time = input().split()
    item = Process(name, int(time))
    q.put(item)

time_sum = 0
while not q.empty():
    pop = q.get()
    if pop.time > Q:
        pop.time = pop.time - Q
        time_sum += Q
        q.put(pop)
    else:
        time_sum += pop.time
        print(pop.name, time_sum)

Solution (Python):
from collections import deque

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

t = 0
done = []
while len(que) != 0:
    name, time = que.popleft()
    mi = min(q, time)
    t += mi
    time -= mi
    if time == 0:
        done.append((name, t))
    else:
        que.append((name, time))
for a in done:
    print(a[0], a[1])

Solution (Python):
from queue import Queue



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

Q=Queue()
for i in range(N):
    Q.put(list(map(str,input().split())))


ans=[]

time=0
while len(ans)!=N:
    p,t=Q.get()
    t=int(t)
    if t-q>0:
        Q.put([p,str(t-q)])
        time+=q
    else:
        time+=t
        ans.append((p,time))

for p,t in ans:
    print(p,t)

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

LEN = 100005
process_list = [None] * LEN
head = 1
tail = n + 1
elaps = 0

class Process:
    def __init__(self, name, time):
        self.name = name
        self.t = time
        
def enqueue(x):
    global process_list, tail
    process_list[tail] = x
    tail = (tail + 1) % LEN
    
def dequeue():
    global process_list, head
    x = process_list[head]
    head = (head + 1) % LEN
    return x
    
def min(a, b):
    a = int(a)
    b = int(b)
    return a if a <= b else b

for i in range(1, n + 1):
    input_list = list(input().split())
    name, time = input_list
    process_list[i] = Process(name, time)


while head != tail:
    u = dequeue()
    c = min(q, u.t)
    u.t = int(u.t) - c
    elaps += c
    if int(u.t) > 0:
        enqueue(u);
    else:
        print('{} {}'.format(u.name, elaps))

Solution (Python):
from collections import deque

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

que = deque(x)
time = 0

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

Solution (Python):
n, q = map(int, input().split(' '))
names = []
rest_time_table = {}
for _ in range(n):
    name_time = input().split(' ')
    name, time = name_time[0], int(name_time[1])
    names.append(name)
    rest_time_table[name] = time

total_time = 0
while True:
    if len(names) == 0:
        break
    name = names.pop(0)
    time = rest_time_table[name]
    rest_time = time - q
    if rest_time <= 0:
        total_time += time
        print(name, total_time)
        continue
    rest_time_table[name] = rest_time
    total_time += q
    names.append(name)

Solution (Python):
from collections import deque, namedtuple


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


def main():
    n, quantum = [int(i) for i in input().split()]
    queue = deque([Process(i, int(j)) for _ in range(n) for (i, j) in [input().split()]])
    completed = []
    time = 0
    while queue:
        processing = queue.popleft()
        delta_time = min(processing.time, quantum)
        processing.time -= delta_time
        time += delta_time
        if processing.time == 0:
            completed.append(Process(processing.name, time))
        else:
            queue.append(processing)
    for process in completed:
        print(process.name, process.time)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque

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

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

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


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

Solution (Python):
a = input().split()
n = int(a[0])
q = int(a[1])
listn = []
listt = []
anslistn = []
anslistt = []
keika = int(0)

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

while listt: 
	num = listt.pop(0) - q
	keika += q
	if num > 0:
		listt.append(num)
		listn.append(listn.pop(0))
	else:
		keika += num
		anslistn.append(listn.pop(0))
		anslistt.append(keika)

i = int(0)

for i in range(n):
	print(str(anslistn[i]) + ' ' + str(anslistt[i]))

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

  def getName(self):
    return self.name

  def getTime(self):
    return self.time

  def runTask(self, q):
    if(self.time > q):
      self.time -= q
      return q
    else:
      tempTime = self.time
      self.time = 0
      return tempTime

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

retAry = []
qTime = 0
que = []

for i in range(n):
  name, time = input().split()
  process = Process(name, int(time))
  que.append(process)

while(len(que)):
  process = que.pop(0)

  qTime += process.runTask(q)
  if(process.getTime() != 0):
    que.append(process)
  else:
    retAry.append(process.getName() + ' ' + str(qTime))

for i in range(len(retAry)):
  print(retAry[i])

Solution (Python):
from collections import deque

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

fin_time = 0
while (time):
    temp_time = time.popleft()
    temp_name = name.popleft()
    fin_time += min(q, temp_time)
    if q >= temp_time:
        print(temp_name + ' ' + str(fin_time))
    else:
        temp_time -= q
        time.append(temp_time)
        name.append(temp_name)

Solution (Python):
from collections import deque

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

Solution (Python):
n, q = tuple(map(int, input().split()))
queue = []
for _ in range(n):
    name, qtime = input().split()
    qtime = int(qtime)
    queue.append((name, qtime))

consumed_time = 0
completed_tasks = []
while True:
    try:
        n_qt = queue.pop(0)
    except:
        break

    name, qtime = n_qt
    if qtime <= q:
        consumed_time += qtime
        completed_tasks.append((name, consumed_time))
    else:
        qtime -= q
        consumed_time += q
        queue.append((name, qtime))

for name, t in completed_tasks:
    print(name, t)

Solution (Python):
n, q = map(int, input().split())
A = []
for _ in range(n):
    name, time = input().split()
    A += [[name, int(time)]]

progress = 0

while A:
    if A[0][1] <= q:
        progress += A[0][1]
        print(A[0][0], progress)
        A.pop(0)
    else:
        progress += q
        A[0][1] -= q
        A += [A[0]]
        A.pop(0)

Solution (Python):
class Process():

    def __init__(self, data):
        self.name = data[0]
        self.time = int(data[1])
        self.endtime = 0

    def process(self, time):
        process_time = min(time, self.time)
        self.time -= process_time
        return process_time

    def is_done(self):
        return self.time <= 0


class MyQueue():

    def __init__(self, max):
        self.Q = [None for _ in range(max)]
        self.head = 0
        self.tail = 0
        self.max = max

    def enqueue(self, x):
        if self.is_full():
            raise ValueError
        self.Q[self.tail] = x
        self.tail += 1
        if self.tail == self.max:
            self.tail = 0

    def dequeue(self):
        if self.is_empty():
            raise IndexError
        ret = self.Q[self.head]
        self.head += 1
        if self.head == self.max:
            self.head = 0
        return ret

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

    def is_full(self):
        return self.head == (self.tail + 1) % self.max

def main():
    n, q = map(int, input().split())
    queue = MyQueue(n + 1)
    current_time = 0
    done = []
    [queue.enqueue(Process(input().split())) for i in range(n)]

    while True:
        try:
            p: Process = queue.dequeue()
        except IndexError:
            break
        process_time = p.process(q)
        current_time += process_time
        if p.is_done():
            p.endtime = current_time
            done.append(p)
        else:
            queue.enqueue(p)

    [print(p.name, p.endtime, sep=" ") for p in done]

if __name__ == "__main__":
    main()

Solution (Python):
nq = list(map(int, input().split()))
n = nq[0]
q = nq[1]

process_list = []
result_list = []

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

now_time = 0
while process_list != []:
    
    [name, time] = process_list.pop(0)
    if time <= q:
        now_time += time
        result_list.append([name, now_time])
    else:
        now_time += q
        process_list.append([name, time-q])

for r in result_list:
    print(r[0]+' '+str(r[1]))

Solution (Python):
from collections import deque

N, Q = map(int, input().split())
names = []
tasks = []

for i in range(N):
    name, task = input().split()
    names.append(name)
    tasks.append(int(task))

names_q = deque(names)
task_q = deque(tasks)

time = 0
while(len(task_q) > 0):
    name = names_q.popleft()
    task = task_q.popleft()
    if task <= Q:
        time += task
        print(name, time)
    else:
        time += Q
        task -= Q
        names_q.append(name)
        task_q.append(task)

Solution (Python):

n, q = list(map(int, input().split()))
data = [input().split() for _ in range(n)]
total_time = 0
ends = []
for name, time in data:
    time = int(time)
    total_time += q if time - q > 0 else time
    time = time - q
    if time > 0:
        data.append([name, time])
        continue
    ends.append([name, total_time])

for name, time in ends:
    print(name, time)

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

A = []

for i in range(n):
  A.append(input())

i = 0

time = 0

while not A == []:
  x,y = map(str, A[i].split())
  y = int(y)
  if y <= q:
    time += y
    times = str(time)
    A[i] = x + " " + times
    print(A.pop(i))
  else:
    z = y-q
    z = str(z)
    time += q
    A.append(x + " " + z)
    del A[i]

Solution (Python):
class MyQueue():
    def __init__(self):
        self.queue = []
    def enqueue(self, item):
        self.queue.append(item)
    def dequeue(self):
        if len(self.queue) == 0:
            return None
        result = self.queue[0]
        del self.queue[0]
        return result

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


myqueue_name = MyQueue()
myqueue_time = MyQueue()
for i in range(n):
    myqueue_name.enqueue(name[i])
    myqueue_time.enqueue(int(time[i]))

count = 0
roop = 0
sum_time = 0
while count != n:
    x = myqueue_name.dequeue()
    y = myqueue_time.dequeue()
    if y <= q:
        count += 1 
        sum_time += y 
        print(x, sum_time)
    else:
        roop += 1
        sum_time += q 
        myqueue_name.enqueue(x)
        myqueue_time.enqueue(y - q)

Solution (Python):
from collections import deque

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

past = 0
while len(que) > 0:
    np = que.popleft()
    if q < np['time']:
        np['time'] = np['time'] - q
        que.append(np)
        past += q
    else:
        past += np['time']
        print(np['name'], past)

Solution (Python):
from collections import deque
n,qms = map(int, input().split())

que = deque([])
for i in range(n):
    name, value = input().split() 
    value = int(value)
    que.append([name,value])
i,t = 0,0

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

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

total = 0

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