A group of $n$ friends decide to go to a restaurant. Each of the friends plans to order meals for $x_i$ burles and has a total of $y_i$ burles ($1 \le i \le n$).

The friends decide to split their visit to the restaurant into several days. Each day, some group of at least two friends goes to the restaurant. Each of the friends visits the restaurant no more than once (that is, these groups do not intersect). These groups must satisfy the condition that the total budget of each group must be not less than the amount of burles that the friends in the group are going to spend at the restaurant. In other words, the sum of all $x_i$ values in the group must not exceed the sum of $y_i$ values in the group.

What is the maximum number of days friends can visit the restaurant?

For example, let there be $n = 6$ friends for whom $x$ = [$8, 3, 9, 2, 4, 5$] and $y$ = [$5, 3, 1, 4, 5, 10$]. Then:

first and sixth friends can go to the restaurant on the first day. They will spend $8+5=13$ burles at the restaurant, and their total budget is $5+10=15$ burles. Since $15 \ge 13$, they can actually form a group.

friends with indices $2, 4, 5$ can form a second group. They will spend $3+2+4=9$ burles at the restaurant, and their total budget will be $3+4+5=12$ burles ($12 \ge 9$).

It can be shown that they will not be able to form more groups so that each group has at least two friends and each group can pay the bill.

So, the maximum number of groups the friends can split into is $2$. Friends will visit the restaurant for a maximum of two days. Note that the $3$-rd friend will not visit the restaurant at all.

Output the maximum number of days the friends can visit the restaurant for given $n$, $x$ and $y$.


-----Input-----

The first line of the input contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$) — the number of friends.

The second line of each test case contains exactly $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_i \le 10^9$). The value of $x_i$ corresponds to the number of burles that the friend numbered $i$ plans to spend at the restaurant.

The third line of each test case contains exactly $n$ integers $y_1, y_2, \dots, y_n$ ($1 \le y_i \le 10^9$). The value $y_i$ corresponds to the number of burles that the friend numbered $i$ has.

It is guaranteed that the sum of $n$ values over all test cases does not exceed $10^5$.


-----Output-----

For each test case, print the maximum number of days to visit the restaurant. If friends cannot form even one group to visit the restaurant, print 0.


-----Examples-----

Input
6
6
8 3 9 2 4 5
5 3 1 4 5 10
4
1 2 3 4
1 1 2 2
3
2 3 7
1 3 10
6
2 3 6 9 5 7
3 2 7 10 6 10
6
5 4 2 1 8 100
1 1 1 1 1 200
6
1 4 1 2 4 2
1 3 3 2 3 4
Output
2
0
1
3
1
3


-----Note-----

The first test case in explained in the problem statement.

In the second test case, friends cannot form at least one group of two or more people.

In the third test case, one way to visit the restaurant in one day is to go in a group of all three friends ($1+3+10 \ge 2+3+7$). Note that they do not have the option of splitting into two groups.

Source: codeforces | Difficulty: EASY
Tags: Sorting

Amortized analysis

Greedy algorithms


Solutions:
def solve():
	n = int(input())
	x = list(map(int, input().split(' ')))
	y = list(map(int, input().split(' ')))
	z = [b - a for (a, b) in zip(x, y)]
	z.sort(reverse=True)
	ans = 0
	(i, j) = (0, n - 1)
	while i < j:
		if z[i] + z[j] >= 0:
			ans += 1
			i += 1
			j -= 1
		else:
			j -= 1
	return ans
for _ in range(int(input())):
	print(solve())


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	diff = [y[i] - x[i] for i in range(n)]
	diff.sort()
	i = 0
	j = n - 1
	res = 0
	while i < j:
		if diff[i] + diff[j] >= 0:
			res += 1
			i += 1
			j -= 1
		else:
			i += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [b[i] - a[i] for i in range(n)]
	c.sort()
	i = 0
	j = n - 1
	s = 0
	while i < j:
		if c[i] + c[j] >= 0:
			s += 1
			j -= 1
		i += 1
	print(s)


def main():
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	arr = [i - j for (i, j) in zip(y, x)]
	arr.sort()
	l = 0
	r = n - 1
	ans = 0
	while l < r:
		if arr[l] + arr[r] >= 0:
			l += 1
			r -= 1
			ans += 1
		else:
			l += 1
	return ans
for _ in range(int(input())):
	print(main())


in_il = lambda : list(map(int, input().split()))
in_sl = lambda : input().split()
in_i = lambda : int(input())
MAX_INT = float('inf')

def solve():
	n = in_i()
	x = in_il()
	y = in_il()
	diff = [0] * n
	for i in range(n):
		diff[i] = y[i] - x[i]
	diff.sort()
	ans = 0
	l = 0
	r = n - 1
	while l < r:
		if diff[l] + diff[r] >= 0:
			ans += 1
			l += 1
			r -= 1
		else:
			l += 1
	print(ans)
for _ in range(in_i()):
	solve()


def solve(A, B):
	arr = [B[i] - A[i] for i in range(len(A))]
	(pp, pn) = ([], [])
	for a in arr:
		if a >= 0:
			pp.append(a)
		else:
			pn.append(a)
	pp.sort()
	pn.sort(reverse=True)
	(i1, i2) = (0, 0)
	res = 0
	while i1 < len(pp):
		if i2 < len(pn):
			if pp[i1] + pn[i2] >= 0:
				i1 += 1
				i2 += 1
				res += 1
				continue
		if i1 < len(pp) - 1:
			i1 += 2
			res += 1
			continue
		i1 += 1
	print(res)
n_case = int(input())
while n_case:
	n_case -= 1
	n = input()
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	solve(a, b)


import sys
from collections import deque
input = sys.stdin.readline
tests = int(input())
for test in range(tests):
	_ = input()
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	diff = []
	for index in range(len(x)):
		diff.append(y[index] - x[index])
	diff.sort(reverse=True)
	diffDeque = deque(diff)
	pairs = 0
	while len(diffDeque) > 1:
		big = diffDeque.popleft()
		while diffDeque:
			smallest = diffDeque.pop()
			if big + smallest >= 0:
				pairs += 1
				break
			else:
				pass
	print(pairs)


import sys
from bisect import bisect_right

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def find_le(a, x):
	i = bisect_right(a, x)
	if i:
		return a[i - 1]
	raise ValueError

def main():
	t = inp()
	for i in range(t):
		ans = 0
		n = inp()
		X = inlt()
		Y = inlt()
		D = [y - x for (x, y) in zip(X, Y)]
		P = [d for d in D if d >= 0]
		N = [-d for d in D if d < 0]
		P.sort()
		N.sort()
		lP = 0
		rP = len(P) - 1
		rN = len(N) - 1
		while lP <= rP:
			f1 = P[rP]
			rP -= 1
			f2 = None
			while rN >= 0:
				if N[rN] <= f1:
					f2 = N[rN]
					rN -= 1
					break
				else:
					rN -= 1
			if f2 is None:
				if lP <= rP:
					f2 = P[lP]
					lP += 1
			if f2 is not None:
				ans += 1
		print(ans, sep='\n')
main()


import sys
import heapq
import math
input = sys.stdin.readline
from collections import Counter, defaultdict

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def main():
	length = inp()
	x = inlt()
	y = inlt()
	diff = [y[i] - x[i] for i in range(length)]
	diff.sort()
	count = 0
	(left, right) = (0, length - 1)
	while left < right:
		if diff[left] + diff[right] >= 0:
			count += 1
			right -= 1
		left += 1
	print(count)
t = inp()
for _ in range(t):
	main()


import sys
sys.setrecursionlimit(10 ** 5)
from collections import deque

def solve():
	N = int(input())
	spend = [int(e) for e in input().split()]
	has = [int(e) for e in input().split()]
	diff = deque(sorted([a - b for (a, b) in zip(has, spend)], reverse=True))
	ans = 0
	while diff:
		big = diff.popleft()
		if big < 0:
			break
		while diff and diff[-1] + big < 0:
			diff.pop()
		if diff and diff[-1] + big >= 0:
			ans += 1
			diff.pop()
	return ans
T = int(input())
for t in range(T):
	s = solve()
	print(s)


import math
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	lst1 = list(map(int, input().split(' ')))
	lst2 = list(map(int, input().split(' ')))
	lst = []
	for i in range(n):
		lst.append(lst2[i] - lst1[i])
	lst.sort(reverse=True)
	ans = 0
	st = 0
	end = n - 1
	while st < end:
		if lst[st] + lst[end] >= 0:
			st += 1
			end -= 1
			ans += 1
		else:
			end -= 1
	print(ans)
t = int(input())
while t:
	t -= 1
	solve()


for _ in range(int(input())):
	n = int(input())
	(x, y) = (list(map(int, input().split())), list(map(int, input().split())))
	ans = 0
	lis = [y[i] - x[i] for i in range(n)]
	lis.sort()
	(left, right) = (0, n - 1)
	while left < right:
		if lis[left] + lis[right] >= 0:
			left += 1
			right -= 1
			ans += 1
		else:
			left += 1
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n,) = I()
	x = I()
	y = I()
	an = 0
	diff = sorted([x[i] - y[i] for i in range(n)])
	i = 0
	j = n - 1
	while i < j:
		if diff[i] + diff[j] <= 0:
			an += 1
			i += 1
			j -= 1
		else:
			j -= 1
	print(an)


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	R()
	c = (a, b) = [[], []]
	for (x, y) in zip(R(), R()):
		y -= x
		c[y < 0] += (abs(y),)
	a.sort()
	b.sort()
	i = j = 0
	l = len(a)
	while j < len(b) and i < l:
		j += a[i] >= b[j]
		i += 1
	print(j + (l - j) // 2)


R = lambda : map(int, input().split())
(t,) = R()
while t:
	t -= 1
	(n,) = R()
	a = sorted((y - x for (x, y) in zip(R(), R())))
	i = 0
	j = n - 1
	while i < j:
		j -= a[i] + a[j] >= 0
		i += 1
	print(n - j - 1)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = sorted([b[i] - a[i] for i in range(n)])
	(f, s) = (0, n - 1)
	ans = 0
	while f < s:
		if c[f] + c[s] >= 0:
			ans += 1
			f += 1
			s -= 1
		else:
			f += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	a = [y[i] - x[i] for i in range(n)]
	a.sort()
	ans = 0
	(l, r) = (0, n - 1)
	while l < r:
		if a[l] + a[r] >= 0:
			r -= 1
			ans += 1
		l += 1
	print(ans)


for t in range(int(input())):
	n = int(input())
	arr = []
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	for i in range(n):
		arr.append(y[i] - x[i])
	arr.sort()
	res = 0
	l = 0
	r = n - 1
	while l < r:
		if arr[l] + arr[r] >= 0:
			res += 1
			r -= 1
		l += 1
	print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	diff = [y[i] - x[i] for i in range(n)]
	diff.sort()
	left = 0
	right = len(diff) - 1
	ans = 0
	while left < right:
		if diff[left] + diff[right] >= 0:
			ans += 1
			left += 1
			right -= 1
			continue
		left += 1
	print(ans)


mukha = lambda : map(int, input().split())
Flag = 0
(mals,) = mukha()
while mals:
	mals -= 1
	(n,) = mukha()
	s = sorted((y - x for (x, y) in zip(mukha(), mukha())))
	i = j = 0
	while i + j + 1 < n:
		j += s[i] + s[~j] >= 0
		i += 1
	print(j)


for tcases in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	kusur = [y[i] - x[i] for i in range(n)]
	kusur.sort()
	(l, r) = (0, n - 1)
	ans = 0
	while l < r:
		while l < r and kusur[l] + kusur[r] < 0:
			l += 1
		if l < r:
			ans += 1
		l += 1
		r -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	c = [b[i] - a[i] for i in range(len(a))]
	c.sort()
	i = 0
	j = n - 1
	cnt = 0
	while i < j:
		if c[i] + c[j] >= 0:
			cnt += 1
			i += 1
			j -= 1
		else:
			i += 1
	print(cnt)


def solve(n, arr1, arr2):
	diff = []
	for i in range(n):
		diff.append(arr2[i] - arr1[i])
	diff.sort()
	m = 0
	i = 0
	j = n - 1
	while i < j:
		if diff[i] + diff[j] >= 0:
			m += 1
			i += 1
			j -= 1
		else:
			i += 1
	return m
t = int(input())
for i in range(t):
	n = int(input())
	arr1 = input().split(' ')
	arr1 = [int(a) for a in arr1]
	arr2 = input().split(' ')
	arr2 = [int(a) for a in arr2]
	ans = solve(n, arr1, arr2)
	print(ans)


testcases = int(input())
for i in range(testcases):
	n = int(input())
	input_1 = input().split()
	list_x = list(map(int, input_1))
	input_2 = input().split()
	list_y = list(map(int, input_2))
	diff = [list_x[i] - list_y[i] for i in range(len(list_x))]
	diff.sort()
	n_visits = 0
	i_left = 0
	i_right = n - 1
	while i_left < i_right:
		if diff[i_left] + diff[i_right] <= 0:
			n_visits += 1
			i_left += 1
			i_right -= 1
		else:
			i_right -= 1
	print(n_visits)


import bisect

def resolve(list_x, list_y):
	neg_list = []
	pos_list = []
	res = 0
	for index in range(len(list_x)):
		sub = list_y[index] - list_x[index]
		if sub >= 0:
			pos_list.append(abs(sub))
		else:
			neg_list.append(abs(sub))
	neg_list = sorted(neg_list)
	pos_list = sorted(pos_list)
	pos_index = len(pos_list) - 1
	for index in range(len(neg_list)):
		if pos_index < 0:
			break
		current_neg = neg_list[-1 - index]
		current_pos = pos_list[pos_index]
		if current_pos >= current_neg:
			res += 1
			pos_index -= 1
	if pos_index >= 0:
		res += int((pos_index + 1) / 2)
	return res

def read_input():
	lines_1 = input()
	lines_2 = input().split(' ')
	list_x = []
	for item in lines_2:
		list_x.append(int(item))
	lines_3 = input().split(' ')
	list_y = []
	for item in lines_3:
		list_y.append(int(item))
	return (list_x, list_y)
case_num = int(input())
if not case_num == 0:
	for index in range(case_num):
		(list_x, list_y) = read_input()
		res = resolve(list_x, list_y)
		print(res)


t = int(input())
for ncase in range(1, t + 1):
	n = int(input())
	x = [int(s) for s in input().split(' ')]
	y = [int(s) for s in input().split(' ')]
	diff = [y[i] - x[i] for i in range(n)]
	diff.sort(reverse=True)
	ans = 0
	i = 0
	j = n - 1
	while i < j:
		if diff[i] < 0:
			break
		elif diff[i] + diff[j] >= 0:
			i += 1
			j -= 1
			ans += 1
		else:
			while diff[i] + diff[j] < 0 and i < j:
				j -= 1
			if diff[i] + diff[j] >= 0 and i < j:
				i += 1
				j -= 1
				ans += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	a = [y[i] - x[i] for i in range(n)]
	a.sort()
	i = 0
	j = n - 1
	ans = 0
	while i < j:
		if a[i] + a[j] >= 0:
			ans += 1
			i += 1
			j -= 1
		else:
			i += 1
	print(ans)


def fun(x, y):
	li = []
	for i in range(len(x)):
		li.append(y[i] - x[i])
	li.sort()
	st = 0
	en = len(li) - 1
	count = 0
	while st < en:
		if li[st] + li[en] >= 0:
			count += 1
			st += 1
			en -= 1
		elif li[st] + li[en] < 0:
			st += 1
	return count
t = int(input())
while t:
	n = int(input())
	x = [int(i) for i in input().split(' ')]
	y = [int(i) for i in input().split(' ')]
	print(fun(x, y))
	t -= 1


for i in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = list()
	for i in range(n):
		ans.append(b[i] - a[i])
	ans.sort()
	(i, j, count) = (0, n - 1, 0)
	while i < j:
		if ans[i] + ans[j] >= 0:
			count += 1
			i += 1
			j -= 1
		else:
			i += 1
	print(count)


for tcase in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	d = sorted((yi - xi for (xi, yi) in zip(x, y)))
	(l, r, ans) = (0, n - 1, 0)
	while l < r:
		while l < r and d[l] + d[r] < 0:
			l += 1
		if l < r:
			ans += 1
		l += 1
		r -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	e = []
	for i in range(n):
		e.append(b[i] - a[i])
	e = sorted(e)
	l = 0
	r = n - 1
	ans = 0
	while l < r:
		if e[l] + e[r] >= 0:
			ans += 1
			r -= 1
			l += 1
		else:
			l += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	val = [arr2[i] - arr1[i] for i in range(n)]
	val.sort()
	ans = 0
	s = 0
	e = n - 1
	while s < e:
		if val[s] + val[e] >= 0:
			ans += 1
			e -= 1
		s += 1
	print(ans)


import sys
import math
from collections import Counter

def gi():
	return list(map(int, sys.stdin.readline().strip().split()))

def gs():
	return sys.stdin.readline().strip()

def fun(a, b):
	l = len(a)
	p = []
	n = []
	c = 0
	for i in range(l):
		b[i] -= a[i]
		if b[i] < 0:
			n.append(b[i])
		else:
			p.append(b[i])
	p.sort()
	pl = len(p)
	n.sort(reverse=True)
	nl = len(n)
	if not nl:
		return pl // 2
	if not p:
		return 0
	j = 0
	for i in range(nl):
		if n[i] != 'x':
			while j < pl:
				if p[j] != 'x' and p[j] + n[i] >= 0:
					p[j] = 'x'
					n[i] = 'x'
					c += 1
					break
				j += 1
	cnt = 0
	for i in p:
		if i != 'x':
			cnt += 1
	return c + cnt // 2
	print(c)
	return c
for _ in range(int(input())):
	a = input()
	a = gi()
	b = gi()
	print(fun(a, b))


def solution(arr1, arr2):
	pos = []
	neg = []
	for i in range(len(arr1)):
		diff = arr2[i] - arr1[i]
		if diff >= 0:
			pos.append(diff)
		else:
			neg.append(-diff)
	pos.sort()
	neg.sort()
	ans = 0
	while pos and neg:
		cur = pos[-1]
		while neg and neg[-1] > cur:
			neg.pop()
		if neg:
			ans += 1
			pos.pop()
			neg.pop()
	print(ans + len(pos) // 2)
t = int(input())
for _ in range(t):
	size = input()
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	solution(arr1, arr2)


def get_data(test_jupy=False):
	if test_jupy:
		data = f.readline()
	else:
		data = input()
	return data
if True:
	t = int(get_data())
	for _ in range(t):
		n = int(get_data())
		x = list(map(int, get_data().split()))
		y = list(map(int, get_data().split()))
		for i in range(n):
			x[i] = y[i] - x[i]
		x.sort()
		k = n - 1
		cnt = 0
		for i in range(n):
			if k <= i:
				break
			if x[i] + x[k] >= 0:
				cnt += 1
				k -= 1
		print(cnt)


def rest():
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	zm = []
	zd = []
	for i in range(n):
		a = y[i] - x[i]
		if a >= 0:
			zm.append(a)
		else:
			zd.append(a)
	zm.sort()
	zd.sort(reverse=True)
	ans = 0
	while len(zm) > 0 and len(zd) > 0:
		d = zd.pop()
		if zm[-1] + d >= 0:
			zm.pop()
			ans += 1
	print(ans + len(zm) // 2)
	return
n = int(input())
for i in range(n):
	rest()


for _ in range(int(input())):
	n = int(input())
	x = [int(i) for i in input().split()]
	y = [int(i) for i in input().split()]
	z = sorted([y - x for (x, y) in zip(x, y)])
	(i, j) = (0, n - 1)
	c = 0
	while i != j and i < j:
		if z[i] + z[j] < 0:
			i += 1
		else:
			i += 1
			j -= 1
			c += 1
	print(c)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	arr = [y[i] - x[i] for i in range(n)]
	arr.sort(reverse=True)
	if arr[1] + arr[0] < 0:
		print(0)
		continue
	(i, j, cnt) = (0, n - 1, 0)
	while i < j:
		if -1 * arr[j] > arr[i]:
			j -= 1
		else:
			cnt += 1
			i += 1
			j -= 1
	print(cnt)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	for i in range(n):
		arr[i] -= brr[i]
	arr.sort()
	(l, r) = (0, n - 1)
	count = 0
	while l < r:
		itr = arr[l] + arr[r]
		if itr > 0:
			r -= 1
		else:
			count += 1
			(l, r) = (l + 1, r - 1)
	print(count)


t = int(input())
for t in range(t):
	n = int(input())
	l = [int(x) for x in input().split()]
	m = [int(x) for x in input().split()]
	r = [m[x] - l[x] for x in range(n)]
	r.sort()
	ans = 0
	i = n - 1
	j = 0
	while j < i:
		if r[i] + r[j] >= 0:
			ans += 1
			i -= 1
			j += 1
		else:
			j += 1
	print(ans)


import sys
input = sys.stdin.readline
n = int(input())
for _ in range(n):
	len = int(input())
	l1 = lambda : list(map(int, input().split()))
	l2 = lambda : list(map(int, input().split()))
	t1 = l1()
	t2 = l2()
	diff = []
	for i in range(len):
		diff.append(t2[i] - t1[i])
	diff.sort()
	j = 0
	i = len - 1
	cnt = 0
	while i > j:
		if diff[i] + diff[j] >= 0:
			cnt += 1
			i -= 1
			j += 1
		else:
			j += 1
	sys.stdout.write(str(cnt) + '\n')


t = int(input())
for _ in range(t):
	f = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	d = 0
	a = [y[i] - x[i] for i in range(f)]
	a.sort(reverse=True)
	(l, h) = (0, f - 1)
	while l < h:
		if a[l] + a[h] >= 0:
			d += 1
			(l, h) = (l + 1, h - 1)
		else:
			h -= 1
	print(d)


tc = int(input())
for _ in range(tc):
	n = int(input())
	x_i = [int(x) for x in input().split()]
	y_i = [int(x) for x in input().split()]
	c_i = [y - x for (x, y) in zip(x_i, y_i)]
	c_i.sort()
	low = 0
	high = len(c_i) - 1
	cnt = 0
	while low < high:
		if c_i[low] + c_i[high] >= 0:
			cnt += 1
			low += 1
			high -= 1
		else:
			low += 1
	print(cnt)


import itertools
import math
import time
from builtins import input, range
from math import gcd as gcd
import sys
import queue
import itertools
import collections
from heapq import heappop, heappush
import random
import os
from random import randint
import decimal
from math import factorial as fac
decimal.getcontext().prec = 26

def solve():
	n = int(sys.stdin.readline())
	x = list(map(int, sys.stdin.readline().split()))
	y = list(map(int, sys.stdin.readline().split()))
	b = [y[i] - x[i] for i in range(n)]
	b.sort()
	res = 0
	r = n - 1
	for l in range(n):
		if l >= r:
			break
		if b[r] + b[l] >= 0:
			r -= 1
			res += 1
	sys.stdout.write(str(res) + '\n')
multi_test = 1
if multi_test == 1:
	t = int(sys.stdin.readline())
	for _ in range(t):
		solve()
else:
	solve()


from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	valu = [y[i] - x[i] for i in range(n)]
	valu.sort(reverse=True)
	i = 0
	j = n - 1
	ans = 0
	while i < j:
		while valu[i] + valu[j] < 0 and i < j:
			j -= 1
		if valu[i] + valu[j] >= 0 and i < j:
			ans += 1
			i += 1
			j -= 1
	print(ans)


from collections import deque
for testcase in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	friends = deque(sorted((y[i] - x[i] for i in range(n))))
	ans = 0
	while len(friends) >= 2:
		r = friends.pop()
		l = friends.popleft()
		while len(friends) > 0 and l + r < 0:
			l = friends.popleft()
		if l + r >= 0:
			ans += 1
	print(ans)


from collections import deque
from itertools import count
t = int(input())
for _ in range(t):
	n = int(input())
	X = [int(i) for i in input().split()]
	Y = [int(i) for i in input().split()]
	guys = deque(sorted((y - x for (x, y) in zip(X, Y))))
	for c in count(1):
		if not guys:
			break
		rich = guys.pop()
		while guys:
			poor = guys.popleft()
			if rich + poor >= 0:
				break
		else:
			break
	print(c - 1)


t = int(input())
result = []
for i in range(t):
	bill = []
	n = int(input())
	xi = list(map(int, input().split(' ')))
	yi = list(map(int, input().split(' ')))
	for j in range(n):
		bill.append(yi[j] - xi[j])
	bill.sort()
	(start, end) = (0, n - 1)
	sum = 0
	while start < end:
		if bill[end] + bill[start] >= 0:
			end -= 1
			sum += 1
		start += 1
	result.append(sum)
for i in result:
	print(i)


def solve():
	n = int(input())
	X = [int(x) for x in input().split()]
	Y = [int(x) for x in input().split()]
	delta = [y - x for (x, y) in zip(X, Y)]
	delta = sorted(delta, reverse=True)
	(l, r) = (0, n - 1)
	pairs = 0
	while True:
		while l < r and delta[r] + delta[l] < 0:
			r -= 1
		if l >= r:
			break
		pairs += 1
		l += 1
		r -= 1
	print(pairs)
	return
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	nap = int(input())
	xa = list(map(int, input().split()))
	ya = list(map(int, input().split()))
	aa = [ya[i] - xa[i] for i in range(nap)]
	aa.sort()
	ans = 0
	(la, ra) = (0, nap - 1)
	while la < ra:
		if aa[la] + aa[ra] >= 0:
			ra -= 1
			ans += 1
		la += 1
	print(ans)


def main():
	total = int(input())
	for i in range(total):
		maximumDays()

def maximumDays():
	answer = 0
	n = int(input())
	burles_wanted = [int(b) for b in input().split(' ')]
	burles_have = [int(h) for h in input().split(' ')]
	match = [burles_have[i] - burles_wanted[i] for i in range(n)]
	match.sort()
	(i, j) = (0, n - 1)
	while i < j:
		if match[i] + match[j] >= 0:
			answer += 1
			i += 1
			j -= 1
		elif match[i] + match[j] < 0:
			i += 1
	print(answer)
main()


for _ in range(int(input())):
	n = int(input())
	if n == 1:
		print(0)
		continue
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	neg = []
	pos = []
	for i in range(n):
		d = b[i] - a[i]
		if d < 0:
			neg.append(d)
		else:
			pos.append(d)
	lp = len(pos)
	if lp == 0:
		print(0)
		continue
	neg.sort()
	pos.sort(reverse=True)
	i = 0
	c = 0
	for k in neg:
		if i >= lp:
			break
		if k + pos[i] >= 0:
			c += 1
			i += 1
	print((lp - c) // 2 + c)


num_of_cases = int(input())
for case in range(num_of_cases):
	n = int(input())
	desires = map(int, input().split())
	budgets = map(int, input().split())
	therich = []
	thepoor = []
	for (budget, desire) in zip(budgets, desires):
		if budget - desire > 0:
			therich.append(budget - desire)
		elif budget - desire < 0:
			thepoor.append(budget - desire)
	satisfied = n - len(thepoor)
	therich.sort(reverse=True)
	thepoor.sort()
	pair = 0
	while thepoor:
		poor = thepoor.pop()
		while therich:
			rich = therich.pop()
			if rich + poor >= 0:
				pair += 1
				break
		else:
			break
	pair += (satisfied - pair) // 2
	print(pair)


t = int(input())
while t > 0:
	n = int(input())
	C = list(map(int, input().split()))
	M = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(M[i] - C[i])
		i += 1
	l = sorted(l, reverse=True)
	i = 0
	j = n - 1
	cnt = 0
	while i < j:
		if l[i] + l[j] >= 0:
			cnt += 1
			i += 1
			j -= 1
		else:
			j = j - 1
	print(cnt)
	t = t - 1


t = int(input())
for _ in range(t):
	l = int(input())
	x = [int(i) for i in input().split()]
	y = [int(i) for i in input().split()]
	s = sorted([x[i] - y[i] for i in range(l)])
	r = l - 1
	res = 0
	l = 0
	while l < r:
		while l < r and s[r] + s[l] > 0:
			r -= 1
		if l < r:
			res += 1
		l += 1
		r -= 1
	print(res)


t = int(input())

def main():
	for q in range(t):
		NUM = int(input())
		ANSWER = list()
		ARRAY = list(map(int, input().split()))
		ARRAY1 = list(map(int, input().split()))
		for iterator in range(NUM):
			ANSWER.append(ARRAY1[iterator] - ARRAY[iterator])
		ANSWER = sorted(ANSWER)
		LEFT = 0
		RIGHT = NUM - 1
		RES = 0
		while LEFT < RIGHT:
			if ANSWER[LEFT] >= 0 and ANSWER[RIGHT] >= 0 or ANSWER[LEFT] * -1 <= ANSWER[RIGHT]:
				LEFT += 1
				RIGHT -= 1
				RES += 1
			else:
				LEFT += 1
		print(RES)
main()


t = int(input())
for _ in range(t):
	n = int(input())
	x = [int(i) for i in input().split()]
	y = [int(i) for i in input().split()]
	need = []
	have = []
	people = 0
	for i in range(n):
		if y[i] >= x[i]:
			people += 1
			have.append(y[i] - x[i])
		else:
			need.append(x[i] - y[i])
	have.sort()
	need.sort()
	(hh, nn) = (len(have), len(need))
	(i, j) = (0, 0)
	while i < hh and j < nn:
		while i < hh and have[i] < need[j]:
			i += 1
		if i < hh:
			people += 1
			i += 1
			j += 1
	print(people // 2)


def solve():
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	ans = 0
	poor = []
	rich = []
	safe = 0
	for i in range(n):
		if y[i] - x[i] < 0:
			poor.append(y[i] - x[i])
		elif y[i] - x[i] > 0:
			rich.append(y[i] - x[i])
		else:
			safe += 1
	poor.sort(key=lambda w: -w)
	rich_left = 0
	rich.sort()
	j = 0
	for i in range(len(rich)):
		if j >= len(poor):
			rich_left += 1
		elif rich[i] + poor[j] >= 0:
			ans += 1
			j += 1
		else:
			rich_left += 1
	print('%d' % (ans + (rich_left + safe) // 2))
t = int(input())
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l3 = []
	for i in range(n):
		l3.append(l2[i] - l1[i])
	l3.sort(reverse=True)
	i = 0
	j = n - 1
	ans = 0
	while i < j:
		if l3[i] + l3[j] >= 0:
			ans += 1
			i += 1
			j -= 1
		else:
			j -= 1
	print(ans)


tt = int(input())
for _ in range(tt):
	n = int(input())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	for i in range(n):
		l1[i] = l2[i] - l1[i]
	l1.sort()
	(j, i) = (0, n - 1)
	ans = 0
	while i > j:
		while j < n and -l1[i] > l1[j]:
			j += 1
		if j >= i:
			break
		ans += 1
		i -= 1
		j += 1
	print(ans)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	duo = []
	for i in range(n):
		duo.append(b[i] - a[i])
	duo.sort()
	i = n - 1
	j = 0
	ans = 0
	while j < i:
		while j < n and -duo[i] > duo[j]:
			j += 1
		if j >= i:
			break
		ans += 1
		i -= 1
		j += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = [int(i) for i in input().split()]
	y = [int(i) for i in input().split()]
	qu = [y[i] - x[i] for i in range(n)]
	qu.sort()
	l = 0
	r = n - 1
	res = 0
	while l < r:
		if qu[l] + qu[r] >= 0:
			l += 1
			r -= 1
			res += 1
		else:
			l += 1
	print(res)


for _ in range(int(input())):
	n = int(input())
	s1 = list(map(int, input().split()))
	s2 = list(map(int, input().split()))
	s3 = []
	for i in range(n):
		s3.append(s2[i] - s1[i])
	s3.sort()
	(l, r, ans) = (0, n - 1, 0)
	while l < r:
		if s3[l] + s3[r] >= 0:
			ans += 1
			l += 1
			r -= 1
		else:
			l += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	dif = [y[i] - x[i] for i in range(n)]
	dif.sort(reverse=True)
	j = n - 1
	ans = 0
	for i in range(n):
		while j > i and dif[i] + dif[j] < 0:
			j -= 1
		if j <= i:
			break
		ans += 1
		j -= 1
	print(ans)


t = int(input())
while t > 0:
	n = int(input())
	x = list(map(int, input().split(' ')))
	y = list(map(int, input().split(' ')))
	diff = [0] * n
	for i in range(n):
		diff[i] = y[i] - x[i]
	diff.sort(reverse=True)
	count = 0
	j = n - 1
	for i in range(n):
		while j >= 0 and diff[i] + diff[j] < 0:
			j -= 1
		if j <= i:
			break
		count += 1
		j -= 1
	print(count)
	t -= 1


from sys import stdin
rd = stdin.readline
for _ in range(int(rd())):
	n = int(rd())
	x = list(map(int, rd().split()))
	y = list(map(int, rd().split()))
	exc = [y[i] - x[i] for i in range(n)]
	exc.sort(reverse=True)
	if exc[1] + exc[0] < 0:
		print(0)
		continue
	(i, j, count) = (0, n - 1, 0)
	while i < j:
		if exc[j] * -1 > exc[i]:
			j -= 1
		else:
			count += 1
			i += 1
			j -= 1
	print(count)


ts = int(input())
ns = []
xs = []
ys = []
for t in range(ts):
	ns.append(int(input()))
	xs.append(list(map(int, input().split())))
	ys.append(list(map(int, input().split())))
for (n, x, y) in zip(ns, xs, ys):
	diffs = [y[i] - x[i] for i in range(n) if y[i] - x[i] != 0]
	zeros = n - len(diffs)
	if zeros % 2 == 1:
		diffs.append(0)
		current_size = n - zeros + 1
		ind_pos = n - 1 - zeros + 1
	else:
		current_size = n - zeros
		ind_pos = n - 1 - zeros
	diffs.sort()
	ind_neg = 0
	pairs = zeros // 2
	done = False
	while current_size > 1:
		if diffs[ind_neg] >= 0:
			pairs += current_size // 2
			done = True
			break
		if diffs[ind_pos] <= 0:
			done = True
			break
		while diffs[ind_pos] < -diffs[ind_neg] and diffs[ind_neg] < 0:
			ind_neg += 1
			current_size -= 1
		if diffs[ind_pos] >= -diffs[ind_neg] and diffs[ind_neg] < 0:
			current_size -= 2
			ind_neg += 1
			ind_pos -= 1
			pairs += 1
	print(pairs)


from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	b = list(map(int, input().split()))
	m = list(map(int, input().split()))
	arr = []
	res = 0
	for (bb, mm) in zip(b, m):
		arr.append(mm - bb)
	arr = sorted(arr, key=lambda x: -x)
	(s, e) = (0, n - 1)
	while s < e:
		if arr[s] + arr[e] >= 0:
			res += 1
			s += 1
			e -= 1
		else:
			e -= 1
	print(res)


import sys
import collections
from math import ceil, gcd, sqrt, log
import bisect
INF = float('inf')
mod = 1000000007

def solve():
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	net = []
	for i in range(n):
		k = B[i] - A[i]
		net.append(k)
	net.sort()
	count = 0
	(i, j) = (0, n - 1)
	while i < j:
		if net[i] + net[j] >= 0:
			count += 1
			i += 1
			j -= 1
		else:
			i += 1
	print(count)
t = int(input())
while t != 0:
	solve()
	t -= 1


T = int(input())
for t in range(0, T):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	arr = [y[i] - x[i] for i in range(0, n)]
	arr.sort()
	ptr1 = 0
	ptr2 = n - 1
	ans = 0
	while ptr1 < ptr2:
		if arr[ptr1] >= 0 and arr[ptr2] >= 0:
			ans += 1
			ptr1 += 1
			ptr2 -= 1
		elif arr[ptr1] < 0 and arr[ptr2] > 0:
			if abs(arr[ptr1]) <= arr[ptr2]:
				ans += 1
				ptr1 += 1
				ptr2 -= 1
			else:
				ptr1 += 1
		else:
			ptr1 += 1
	print(ans)


from collections import defaultdict
from sys import stdin

def solution(n, A, B):
	diffs = [b - a for (a, b) in zip(A, B)]
	sorted_diffs = sorted(diffs, reverse=True)
	left = 0
	right = n - 1
	result = 0
	while left < right:
		if sorted_diffs[left] + sorted_diffs[right] >= 0:
			result += 1
			left += 1
		right -= 1
	return result
test_cases = int(stdin.readline())
for it in range(test_cases):
	n = int(stdin.readline().strip())
	A = [int(x) for x in stdin.readline().split()]
	B = [int(x) for x in stdin.readline().split()]
	print(solution(n, A, B))


from array import array
for i in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	ans = []
	for i in range(n):
		ans.append(y[i] - x[i])
	ans.sort()
	res = 0
	(l, r) = (0, n - 1)
	while l < r:
		if ans[l] + ans[r] >= 0:
			r -= 1
			res += 1
		l += 1
	print(res)


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	canSpend = []
	for (i, j) in zip(x, y):
		canSpend.append(j - i)
	canSpend.sort()
	(l, r) = (0, len(canSpend) - 1)
	ans = 0
	while l <= r:
		if canSpend[r] < 0:
			break
		if canSpend[l] >= 0:
			ans += (r - l + 1) // 2
			break
		if canSpend[l] + canSpend[r] >= 0:
			ans += 1
			l += 1
			r -= 1
		else:
			while l <= r and canSpend[l] < 0 and (canSpend[l] + canSpend[r] < 0):
				l += 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	D = [B[i] - A[i] for i in range(n)]
	D.sort(reverse=True)
	r = n - 1
	ans = 0
	for l in range(n):
		while l < r and D[l] + D[r] < 0:
			r -= 1
		if l >= r:
			break
		ans += 1
		r -= 1
	print(ans)


for _ in range(int(input())):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	t = []
	for i in range(n):
		t.append(y[i] - x[i])
	t.sort()
	teams = 0
	l = 0
	r = n - 1
	while l < r:
		if t[r] + t[l] >= 0:
			teams += 1
			r -= 1
		l += 1
	print(teams)


t = int(input())
for i in range(t):
	n = int(input())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	rich = []
	poor = []
	smart = 0
	for j in range(len(x)):
		d = x[j] - y[j]
		if d > 0:
			poor.append(d)
		elif d == 0:
			smart += 1
		else:
			rich.append(abs(d))
	poor.sort()
	rich.sort()
	res = smart // 2
	while len(poor) != 0 and len(rich) != 0:
		if poor[-1] > rich[-1]:
			poor.pop()
		else:
			res += 1
			rich.pop()
			poor.pop()
	res += len(rich) // 2
	res += (len(rich) % 2 + smart % 2) // 2
	print(res)