Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:  the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes,  Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. 

Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.

For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house.

Find the minimum time after which all the dishes can be at Petya's home.


-----Input-----

The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. Then $t$ test cases follow.

Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of dishes that Petya wants to order.

The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$) — the time of courier delivery of the dish with the number $i$.

The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$) — the time during which Petya will pick up the dish with the number $i$.

The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.


-----Output-----

For each test case output one integer — the minimum time after which all dishes can be at Petya's home.


-----Example-----
Input
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2

Output
5
3
2
3

Difficulty: interview

Starter code:

Solutions:
def check(M):
    sm = 0
    for i in range(n):
        if a[i] > M:
            sm += b[i]
    return sm <= M


gans = []
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    L = 0
    R = max(a)
    while R - L > 1:
        M = (L + R) // 2
        if check(M):
            R = M
        else:
            L = M
    gans.append(R)
print(*gans, sep='\n')


def helper(m):
	s = 0
	for i in range(len(a)):
		if a[i] > m:
			s += b[i]
	return s <= m

for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	l, r = 1, 10**9

	while l < r:
		mid = l + (r-l)//2
		temp = helper(mid)

		if temp:
			r = mid
		else:
			l = mid+1
	print(l)




import sys
input=sys.stdin.readline
def f(x):
    ans=0
    for i in range(n):
        if(a[i]<=x):
            continue
        ans+=b[i]
    if(ans<=x):
        return 1
    return 0
def bsearch(l,r):
    m=(l+r)//2
    if(f(m)):
        if(f(m-1)==0):
            return m
        return bsearch(l,m-1)
    return bsearch(m+1,r)
t=int(input())
for you in range(t):
    n=int(input())
    l=input().split()
    a=[int(i) for i in l]
    l=input().split()
    b=[int(i) for i in l]
    print(bsearch(0,10**9+5))


for _ in range(int(input())):
	 n = int(input())
	 a = [*list(map(int, input().split()))]
	 b = [*list(map(int, input().split()))]
	 lo = 0
	 hi = sum(a)
	 while lo < hi:
	 	mid = (lo + hi) // 2
	 	if sum(y if x > mid else 0 for x,y in zip(a,b)) <= mid:
	 		hi = mid
	 	else:
	 		lo = mid + 1
	 print(lo)


def f(aa, bb, target):
    total = 0
    for i in range(len(aa)):
        if aa[i] > target:
            total += bb[i]

    return total <= target


t = int(input())

for case in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    low = 1
    high = 1000000000

    while low < high:
        mid = (low + high) // 2
        res = f(a, b, mid)
        if res:
            # mid is possible
            high = mid
        else:
            low = mid + 1

    print(low)


"""T=int(input())
for _ in range(0,T):
    n=int(input())
    a,b=map(int,input().split())
    s=input()
    s=[int(x) for x in input().split()]
    for i in range(0,len(s)):
        a,b=map(int,input().split())"""



T=int(input())
for _ in range(0,T):
    n=int(input())
    a=[int(x) for x in input().split()]
    b=[int(x) for x in input().split()]
    low=0
    high=max(a)
    ans=max(a)
    while(low<=high):
        mid=(low+high)>>1
        tot=0
        for i in range(0,len(a)):
            if(a[i]>mid):
                tot+=b[i]

        if(tot<=mid):
            ans=min(ans, mid)
            high=mid-1
        else:
            low=mid+1
    print(ans)
            



import sys

def input():
    
    return sys.stdin.readline()
    
    
for _ in range(int(input())):
    
    n = int(input())
    
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    x = 10 ** 9
    y = 0
    while x > y + 1:
        
        z = (x + y) // 2
        
        t = 0
        
        for i in range(n):
            if A[i] > z:
                t += B[i]
        
        if t > z:
            y = z
        
        else:
            x = z
            
    print(x)
    
        


import sys
import math
def II():
	return int(sys.stdin.readline())
 
def LI():
	return list(map(int, sys.stdin.readline().split()))
 
def MI():
	return list(map(int, sys.stdin.readline().split()))
 
def SI():
	return sys.stdin.readline().strip()
t = II()
for q in range(t):
	n = II()
	a = LI()
	b = LI()
	b = sorted(enumerate(b), key=lambda x: a[x[0]])
	b = [i[1] for i in b]
	a.sort()
	x = []
	s = 0
	for i in range(n-1,-1,-1):
		s+=b[i]
		x.append(s)
	x = x[:][::-1]
	ans = s
	for i in range(n):
		if i == n-1:
			ans = min(ans,a[i])
		else:
			ans = min(ans,max(a[i],x[i+1]))
	print(ans)


def read_generator():
    while True:
        tokens = input().split(' ')
        for t in tokens:
            yield t

reader = read_generator()

def readword():
    return next(reader)

def readint():
    return int(next(reader))

def readfloat():
    return float(next(reader))

def readline():
    return input()

def solve(a, b, n):
    l = 1
    r = 10 ** 9

    while r - l > 1:
        t = (l + r) // 2
        if possible(a, b, n, t):
            r = t
        else:
            l = t

    if possible(a, b, n, l):
        return l
    return r


def possible(a, b, n, t):
    s = 0
    for i in range(n):
        if a[i] > t:
            s += b[i]
    return s <= t

tests = readint()

for t in range(tests):
    n = readint()
    a = [readint() for _ in range(n)]
    b = [readint() for _ in range(n)]
    print(solve(a, b, n))

import sys
input = iter(sys.stdin.read().splitlines()).__next__

t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    times = list(zip(a, b))
    times.sort()
    # print(times)
    pickup_time = sum(b)
    best_time = pickup_time
    for num_deliveries in range(1, n+1):
        pickup_time -= times[num_deliveries-1][1]
        delivery_time = times[num_deliveries-1][0]
        best_time = min(best_time, max(pickup_time, delivery_time))
        if pickup_time < delivery_time:
            break
    print(best_time)

def xxx(x):
    nonlocal a,b
    res=0
    for i in range(len(a)):
        if a[i]>x:
            res+=b[i]
    if res<=x:
        return True
    else:
        return False

for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    i1=0
    i2=sum(b)
    while i2-i1>1:
        m=(i1+i2)//2
        if xxx(m):
            i2=m
        else:
            i1=m
    print(i2)