Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.

Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came.

The i-th request is characterized by two values: s_{i} — the day when a client wants to start the repair of his car, d_{i} — duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on.

Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows:  If the car repair shop is idle for d_{i} days starting from s_{i} (s_{i}, s_{i} + 1, ..., s_{i} + d_{i} - 1), then these days are used to repair a car of the i-th client.  Otherwise, Polycarp finds the first day x (from 1 and further) that there are d_{i} subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + d_{i} - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + d_{i} - 1]. It is possible that the day x when repair is scheduled to start will be less than s_{i}. 

Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.


-----Input-----

The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients.

The following n lines contain requests, one request per line. The i-th request is given as the pair of integers s_{i}, d_{i} (1 ≤ s_{i} ≤ 10^9, 1 ≤ d_{i} ≤ 5·10^6), where s_{i} is the preferred time to start repairing the i-th car, d_{i} is the number of days to repair the i-th car.

The requests should be processed in the order they are given in the input.


-----Output-----

Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.


-----Examples-----
Input
3
9 2
7 3
2 4

Output
9 10
1 3
4 7

Input
4
1000000000 1000000
1000000000 1000000
100000000 1000000
1000000000 1000000

Output
1000000000 1000999999
1 1000000
100000000 100999999
1000001 2000000

Difficulty: MEDIUM_HARD
Tags: Implementation

Starter code:

Solutions:
(t, p) = (1, [])
for i in range(int(input())):
	(l, d) = map(int, input().split())
	if t > l:
		for (i, q) in enumerate(p, 1):
			if q[0] <= l <= q[1] - d:
				p.insert(i, [l + d, q[1]])
				q[1] = l
				break
		else:
			for q in p:
				if q[0] <= q[1] - d:
					l = q[0]
					q[0] += d
					break
			else:
				l = t
				t += d
	else:
		p.append([t, l])
		t = l + d
	print(l, l + d - 1)


n = int(input())
L = []
for j in range(n):
	ch = input().split()
	(s, d) = (int(ch[0]), int(ch[1]))
	if j == 0:
		print(s, d + s - 1)
		L.append([s, s + d - 1])
		L.sort()
	else:
		B = True
		C = True
		for i in range(len(L)):
			if i < len(L) - 1 and s > L[i][1] and (s + d - 1 < L[i + 1][0]):
				print(s, s + d - 1)
				L.append([s, s + d - 1])
				L.sort()
				C = False
				break
			if L[i][1] >= s >= L[i][0] or L[i][0] <= s + d - 1 <= L[i][1] or (s <= L[i][0] and s + d - 1 >= L[i][1]):
				B = False
				break
		if B and C:
			print(s, s + d - 1)
			L.append([s, s + d - 1])
			L.sort()
			C = False
		if C:
			if d < L[0][0]:
				print(1, d)
				L.append([1, d])
				L.sort()
				C = False
			else:
				for i in range(len(L)):
					if i < len(L) - 1 and L[i][1] + d < L[i + 1][0]:
						print(L[i][1] + 1, L[i][1] + d)
						L.append([L[i][1] + 1, L[i][1] + d])
						L.sort()
						C = False
						break
		if not B and C:
			print(L[len(L) - 1][1] + 1, L[len(L) - 1][1] + d)
			L.append([L[len(L) - 1][1] + 1, L[len(L) - 1][1] + d])
			L.sort()


def main():
	try:
		while True:
			n = int(input())
			req = [tuple(map(int, input().split())) for i in range(n)]
			used = [(req[0][0], req[0][0] + req[0][1])]
			print(used[0][0], used[0][1] - 1)
			for (start, dur) in req[1:]:
				last = 1
				for (a, b) in used:
					if last <= start and start + dur <= a:
						used.append((start, start + dur))
						used.sort()
						print(start, start + dur - 1)
						break
					last = b
				else:
					if start >= used[-1][1]:
						used.append((start, start + dur))
						used.sort()
						print(start, start + dur - 1)
					else:
						last = 1
						for (a, b) in used:
							if a - last >= dur:
								used.append((last, last + dur))
								used.sort()
								break
							last = b
						else:
							used.append((last, last + dur))
						print(last, last + dur - 1)
	except EOFError:
		pass
main()


import sys
lines = iter(sys.stdin.read().splitlines())
next(lines)
(s, d) = map(int, next(lines).split())
dates = [[0, 0], [s, s + d - 1], [10000000001, 10000000001]]
res = [[s, s + d - 1]]
for line in lines:
	(s, d) = map(int, line.split())
	nhueco = True
	for i in range(len(dates)):
		if s > dates[i][1] and s + d - 1 < dates[i + 1][0]:
			dates.insert(i + 1, [s, s + d - 1])
			res.append([s, s + d - 1])
			break
		elif nhueco and -dates[i][1] + dates[i + 1][0] - 1 >= d:
			nhueco = False
			ld = dates[i][1] + 1
			li = i + 1
	else:
		dates.insert(li, [ld, ld + d - 1])
		res.append([ld, ld + d - 1])
for date in res:
	print(' '.join(map(str, date)))


n = int(input())
cur = []

def good(s, e):
	if s < 1:
		return False
	assert s <= e
	for (l, r) in cur:
		if max(l, s) <= min(r, e):
			return False
	return True
for i in range(n):
	(s, d) = map(int, input().split())
	e = s + d - 1
	if not good(s, e):
		s = int(2000000000.0)
		if good(1, d):
			s = 1
		for (l, r) in cur:
			if good(l - d, l - 1):
				s = min(s, l - d)
			if good(r + 1, r + d):
				s = min(s, r + 1)
	cur.append((s, s + d - 1))
	cur.sort()
	print(s, s + d - 1)


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] < key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
n = int(input())
l = []
for i in range(n):
	(s, d) = map(int, input().split())
	l.append((s, d))
ans = []
heapq.heapify(ans)
for i in range(n):
	(s, d) = l[i]
	f = 0
	for j in range(len(ans)):
		if s > ans[j][1] or s + d - 1 < ans[j][0]:
			continue
		else:
			f = 1
			break
	if f == 0:
		print(s, s + d - 1)
		heapq.heappush(ans, (s, s + d - 1))
		continue
	res = []
	e = heapq.heappop(ans)
	if d < e[0]:
		print(1, d)
		heapq.heappush(ans, e)
		heapq.heappush(ans, (1, d))
		continue
	heapq.heappush(ans, e)
	while len(ans) > 0:
		e = heapq.heappop(ans)
		e1 = (9999999999999999999, 9999999999999999999)
		if len(ans) > 0:
			e1 = heapq.heappop(ans)
		if e[1] + 1 + d - 1 < e1[0]:
			print(e[1] + 1, e[1] + 1 + d - 1)
			heapq.heappush(ans, (e[1] + 1, e[1] + 1 + d - 1))
			res.append(e)
			if e1[0] != 9999999999999999999:
				heapq.heappush(ans, e1)
			break
		res.append(e)
		if e1[0] != 9999999999999999999:
			heapq.heappush(ans, e1)
	for i in res:
		heapq.heappush(ans, i)


n = int(input())
(l, r) = ([0] * n, [0] * n)
f = lambda x, y: all((x > r[j] or y < l[j] for j in range(i)))
for i in range(n):
	(x, d) = map(int, input().split())
	y = x + d - 1
	if not f(x, y):
		k = min((r[j] for j in range(i + 1) if f(r[j] + 1, r[j] + d)))
		(x, y) = (k + 1, k + d)
	(l[i], r[i]) = (x, y)
	print(x, y)


def dotwointervals(l1, r1, l2, r2):
	if l1 < l2 and r1 < l2:
		return 0
	elif l1 > r2 and r1 > r2:
		return 0
	return 1
n = int(input())
lofdays = []
for you in range(n):
	l = input().split()
	si = int(l[0])
	di = int(l[1])
	if you == 0:
		lofdays.append((si, si + di - 1))
		print(si, si + di - 1)
	else:
		nowint = (si, si + di - 1)
		done = 1
		for i in lofdays:
			if dotwointervals(nowint[0], nowint[1], i[0], i[1]):
				done = 0
				break
		if done == 1:
			lofdays.append(nowint)
			print(si, si + di - 1)
		else:
			mina = min(lofdays)
			if mina[0] - di > 0:
				print(1, di)
				lofdays.append((1, di))
			else:
				lofdays.sort()
				done = 0
				for i in range(1, len(lofdays)):
					if lofdays[i][0] - lofdays[i - 1][1] - 1 >= di:
						done = 1
						print(lofdays[i - 1][1] + 1, lofdays[i - 1][1] + di)
						lofdays.append((lofdays[i - 1][1] + 1, lofdays[i - 1][1] + di))
						break
				if done == 0:
					print(lofdays[-1][1] + 1, lofdays[-1][1] + di)
					lofdays.append((lofdays[-1][1] + 1, lofdays[-1][1] + di))


from bisect import bisect_left, insort_left
a = []
n = int(input())
for _ in range(n):
	(s, d) = map(int, input().split())
	if len(a) == 0:
		print(s, s + d - 1)
		a.append((s, s + d - 1))
		continue
	p = bisect_left(a, (s, s + d - 1))
	ok = True
	if p > 0 and a[p - 1][1] >= s:
		ok = False
	if p < len(a) and a[p][0] <= s + d - 1:
		ok = False
	if ok:
		insort_left(a, (s, s + d - 1))
		print(s, s + d - 1)
	else:
		ok = False
		for i in range(len(a)):
			if i == 0:
				if a[0][0] > d:
					print(1, d)
					a = [(1, d)] + a
					ok = True
					break
			elif a[i - 1][1] + d < a[i][0]:
				print(a[i - 1][1] + 1, a[i - 1][1] + d)
				insort_left(a, (a[i - 1][1] + 1, a[i - 1][1] + d))
				ok = True
				break
		if not ok:
			print(a[-1][1] + 1, a[-1][1] + d)
			insort_left(a, (a[-1][1] + 1, a[-1][1] + d))