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

Starter code:

Solutions:
from bisect import bisect_left, insort_left
a = []
n = int(input())
for _ in range(n):
    #print(a)
    s, d = list(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))
    #print('p', p)
    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
            else:
                if 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))


#!/usr/bin/env python3


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))
                            # used.sort()
                        print(last, last + dur - 1)

    except EOFError:
        pass


main()


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


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

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

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)

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

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(2e9)
		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)

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