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):
class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 100010
        self.queue = []
        counter = 0
        while counter < self.length:
            self.queue.append(Process())
            counter += 1
        self.head = 0
        self.tail = 0

    def enqueue(self, name, time):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        
        
        
        self.queue[self.tail].name = name
        self.queue[self.tail].time = time
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        
        
        
        self.queue[self.head].name = ""
        self.queue[self.head].time = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        name, time = my_queue.queue[my_queue.head].name, \
                     my_queue.queue[my_queue.head].time
        my_queue.dequeue()
        my_queue.enqueue(name, time)

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()
    my_queue.enqueue(name, int(time))
    counter += 1

current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = []
        counter = 0
        while counter < self.length:
            self.queue.append(Process())
            counter += 1
        self.head = 0
        self.tail = 0

    def enqueue(self, name, time):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail].name = name
        self.queue[self.tail].time = time
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        self.queue[self.head].name = ""
        self.queue[self.head].time = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        name, time = my_queue.queue[my_queue.head].name, \
                     my_queue.queue[my_queue.head].time
        my_queue.dequeue()
        my_queue.enqueue(name, time)

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()
    my_queue.enqueue(name, int(time))
    counter += 1

current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = []
        counter = 0
        while counter < self.length:
            self.queue.append(Process())
            counter += 1
        self.head = 0
        self.tail = 0

    def enqueue(self, name, time):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail].name = name
        self.queue[self.tail].time = time
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        self.queue[self.head].name = ""
        self.queue[self.head].time = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time, end_time_list):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        end_time_list.append([my_queue.queue[my_queue.head].name, current_time])
        
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        name, time = my_queue.queue[my_queue.head].name, \
                     my_queue.queue[my_queue.head].time
        my_queue.dequeue()
        my_queue.enqueue(name, time)

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()
    my_queue.enqueue(name, int(time))
    counter += 1

end_time_list = []
current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time, end_time_list)

for data in end_time_list:
    print data[0], data[1]

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = [0] * self.length
        
        
        
        
        self.head = 0
        self.tail = 0

    
    def enqueue(self, process):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail] = process
        
        
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        
        
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time,):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        name, time = my_queue.queue[my_queue.head].name, \
                     my_queue.queue[my_queue.head].time
        my_queue.enqueue(my_queue.queue[my_queue.head])
        my_queue.dequeue()

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()

    my_queue.enqueue(Process(name, int(time)))
    counter += 1


current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = [0] * self.length
        
        
        
        
        self.head = 0
        self.tail = 0

    
    def enqueue(self, process):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail] = process
        
        
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        
        
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time,):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        name, time = my_queue.queue[my_queue.head].name, \
                     my_queue.queue[my_queue.head].time
        my_queue.enqueue(my_queue.queue[my_queue.head])
        my_queue.dequeue()

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()

    my_queue.enqueue(Process(name, int(time)))
    counter += 1


current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = [0] * self.length
        
        
        
        
        self.head = 0
        self.tail = 0

    
    def enqueue(self, process):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail] = process
        
        
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        
        
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time,):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        
        
        my_queue.enqueue(my_queue.queue[my_queue.head])
        my_queue.dequeue()

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()

    my_queue.enqueue(Process(name, int(time)))
    counter += 1


current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50010
        self.queue = [0] * self.length
        
        
        
        
        self.head = 0
        self.tail = 0

    
    def enqueue(self, process):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail] = process
        
        
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        
        
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time,):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """

    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        
        
        my_queue.enqueue(my_queue.queue[my_queue.head])
        my_queue.dequeue()

    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]
counter = 0
while counter < n:
    name, time = raw_input().split()

    my_queue.enqueue(Process(name, int(time)))
    counter += 1


current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import time as ti


class MyQueue(object):
    """
    My Queue class

    Attributes:
        queue: queue
        head
        tail
    """

    def __init__(self):
        """Constructor
        """
        self.length = 50005
        self.queue = [0] * self.length
        self.head = 0
        self.tail = 0

    
    def enqueue(self, process):
        """enqueue method

        Args:
            name: enqueued process name
            time: enqueued process time

        Returns:
            None
        """
        self.queue[self.tail] = process
        self.tail = (self.tail + 1) % self.length

    def dequeue(self):
        """dequeue method

        Returns:
            None
        """
        self.queue[self.head] = 0
        self.head = (self.head + 1) % self.length

    def is_empty(self):
        """check queue is empty or not

        Returns:
            Bool
        """
        if self.head == self.tail:
            return True
        else:
            return False

    def is_full(self):
        """chech whether queue is full or not"""
        if self.tail - self.head >= len(self.queue):
            return True
        else:
            return False


class Process(object):
    """process class
    """

    def __init__(self, name="", time=0):
        """constructor
        Args:
            name: name
            time: time
        """
        self.name = name
        self.time = time

    def forward_time(self, time):
        """time forward method

        Args:
            time: forward time interval

        Returns:
            remain time

        """
        self.time -= time
        return self.time


def time_forward(my_queue, interval, current_time):
    """

    Args:
        my_queue: queue
        interval: time step interval
        current_time: current time
    """
    value = my_queue.queue[my_queue.head].forward_time(interval)
    if value <= 0:
        current_time += (interval + value)
        print my_queue.queue[my_queue.head].name, current_time
        my_queue.dequeue()

    elif value > 0:
        current_time += interval
        my_queue.enqueue(my_queue.queue[my_queue.head])
        my_queue.dequeue()
    return current_time


my_queue = MyQueue()
n, q = [int(x) for x in raw_input().split()]

counter = 0
while counter < n:
    name, time = raw_input().split()
    my_queue.enqueue(Process(name, int(time)))
    counter += 1

current_time = 0
while not my_queue.is_empty():
    current_time = time_forward(my_queue, q, current_time)

Solution (Python):
import collections
n,q = map(int, input().split())
queue = collections.deque(maxlen=100000)

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

time = 0
while queue:
    p,t = queue.popleft()
    if t<=q:
        time += t
        print(p,time)
    else:
        time += q
        queue.append([p,t-q])

Solution (Python):

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

while len(A)!=0:
	if(int(A[0][1])>int(M)):
		A[0][1]=str(int(A[0][1])-int(M))
		A.append(A[0])
		del A[0]
		time+=int(M)
	else:
		time+=int(A[0][1])
		print A[0][0]+" "+str(time)
		del A[0]

Solution (Python):
def do_round_robin(process, tq):
    from collections import deque
    q = deque()
    elapsed_time = 0  
    finished_process = []  

    
    for process_name, remaining_time in process:
        q.append([process_name, int(remaining_time)])

    while True:
        try:
            process_name, remaining_time = q.pop()
        except IndexError:  
            break
        else:
            elapsed_time += min(remaining_time, tq)
            remaining_time -= tq
            if remaining_time > 0:
                q.appendleft([process_name, remaining_time])
            else:
                finished_process.append((process_name, elapsed_time))
    return finished_process


if __name__ == '__main__':
    
    
    data = [int(x) for x in input().split(' ')]
    num_of_process = data[0]
    time_quantum = data[1]
    process = []
    for i in range(num_of_process):
        process.insert(0, [x for x in input().split(' ')])

    
    results = do_round_robin(process, time_quantum)

    
    for i in results:
        print('{0} {1}'.format(i[0], i[1]))

Solution (Python):
def do_round_robin(process, tq):
    
    from collections import deque
    q = deque()
    elapsed_time = 0  
    finished_process = []  
    for process_name, remaining_time in process:
        q.append([process_name, int(remaining_time)])  
        
    
    while True:
        try:
            process_name, remaining_time = q.pop()
        except IndexError:  
            break
        else:
            elapsed_time += min(remaining_time, tq)
            remaining_time -= tq
            if remaining_time > 0:
                q.appendleft([process_name, remaining_time])  
            else:
                finished_process.append((process_name, elapsed_time))  
    return finished_process


if __name__ == '__main__':
    
    
    data = [int(x) for x in input().split(' ')]
    num_of_process = data[0]
    time_quantum = data[1]
    process_table = []
    for i in range(num_of_process):
        process_table.insert(0, [x for x in input().split(' ')])

    
    results = do_round_robin(process_table, time_quantum)

    
    for process_name, finish_time in results:
        print('{0} {1}'.format(process_name, finish_time))

Solution (Python):
from collections import deque

n,p=map(int ,input().split())
que=deque([])

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

t=0
while len(que)>0:
    atop=que.popleft()
    spend=min(atop[1],p)
    atop[1]-=spend
    t+=spend
    if(atop[1]==0):
        print(atop[0],t)
    else:
        que.append(atop)

Solution (Python):



''' ?????\??? '''




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

    while len(process_name) > 0:
        p_name = process_name.pop(0)
        p_time = process_time.pop(0)

        if p_time > quantum:
            elapsed_time += quantum
            process_name.append(p_name)
            process_time.append(p_time - quantum)
        else:
            elapsed_time += p_time
            result.append(p_name + ' ' + str(elapsed_time))

    return result


if __name__ == '__main__':
    N_p, quantum = map(int, input().split())
    p_name, p_time = [], []
    for i in range(N_p):
        p = list(input().split())
        p_name.append(p[0])
        p_time.append(int(p[1]))

    result = round_robin_scheduling(p_name, p_time, quantum)

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

Solution (Python):


queue = list()
time = 0

n, q = [int(x) for i, x in enumerate(input().split()) if i < 2]
for i in range(n):
    temp = input().split()
    temp[1] = int(temp[1])
    queue.append(temp)
while(len(queue)):
    process = queue.pop(0)
    if process[1] <= q:
        time += process[1]
        print("{0} {1}".format(process[0], time))
    else:
        time += q
        process[1] -= q
        queue.append(process)

Solution (Python):
queue = []

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

t = 0
while len(queue) > 0:
  entry = queue.pop(0)
  t += min(entry[1], q)
  entry[1] -= q
  if entry[1] > 0:
    queue.append(entry)
  else:
    print("{} {}".format(entry[0], t))

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):
n, q = map(int, input().split())
tasks = [(li[0], int(li[1])) for li in [input().split() for _ in range(n)]]

elapsed = 0
while len(tasks):
    name, time = tasks.pop(0)
    if time > q:
        elapsed += q
        tasks.append((name, time - q))
    else:
        elapsed += time
        print('{0} {1}'.format(name, elapsed))

Solution (Python):

from collections import deque

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

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

total = 0

while procs:
    procs[0][1] -= q

    if procs[0][1] > 0:
        total += q
        procs.append(procs.popleft())
    else:
        total += q + procs[0][1]
        print(procs[0][0], total)
        procs.popleft()

Solution (Python):
n, q = list(map(int, input().split()))
que = []
for _ in range(n):
    name, time = input().split()
    que.append([name, int(time)])
    
elapsed_time = 0
while que:
    name, time = que.pop(0)
    dt = min(q, time)
    time -= dt
    elapsed_time += dt
    if time > 0:
        que.append([name, time])
    else:
        print (name, elapsed_time)

Solution (Python):
from collections import deque

n, q = list(map(int, input().split()))
que = deque([])
for _ in range(n):
    name, time = input().split()
    que.append([name, int(time)])
    
elapsed_time = 0
while que:
    name, time = que.popleft()
    dt = min(q, time)
    time -= dt
    elapsed_time += dt
    if time > 0:
        que.append([name, time])
    else:
        print (name, elapsed_time)

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

queue = []

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

for i in range(n):
    [name, str_time] = input().split()
    time = int(str_time)
    queue.insert(0, Task(name, time))


elps = 0

while queue != []:
    one = queue.pop()
    a = min(one.time, q)
    one.time -= a
    elps += a
    if one.time > 0:
        queue.insert(0, one)
    else:
        print(one.name, elps)

Solution (Python):
def main():
    process, quantum = input().split()
    process = int(process)
    quantum = int(quantum)
    processes = []
    for i in range(process):
        t, u = input().split()
        processes.append((t, int(u)))
    ptime = 0
    while len(processes) > 0:
        t = processes.pop(0)
        if t[1] > quantum:
            processes.append((t[0], t[1]-quantum))
            ptime += quantum
        else:
            ptime += t[1]
            print(t[0] + " " + str(ptime))




if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Solution (Python):
n, q = map(int, input().split())
p = [input().split() for _ in range(n)]
t = i = 0
while n:
    i = i % n
    if int(p[i][1]) <= q:
        t += int(p[i][1])
        print(p[i][0], t)
        del p[i]
        n -= 1
    else:
        p[i][1] = int(p[i][1]) - q
        t += q
        i += 1