Alexa has two stacks of non-negative integers, stack $a[n]$ and stack $b[m]$ where index $0$ denotes the top of the stack. Alexa challenges Nick to play the following game:

In each move, Nick can remove one integer from the top of either stack $\class{ML__boldsymbol}{\boldsymbol{a}}$ or stack $\boldsymbol{b}$.
Nick keeps a running sum of the integers he removes from the two stacks.
Nick is disqualified from the game if, at any point, his running sum becomes greater than some integer $maxSum$ given at the beginning of the game.
Nick's final score is the total number of integers he has removed from the two stacks.

Given $\class{ML__boldsymbol}{\boldsymbol{a}}$, $\boldsymbol{b}$, and $maxSum$ for $\mathrm{~g~}$ games, find the maximum possible score Nick can achieve.

Example 

$a=[1,2,3,4,5]$ 

$b=[6,7,8,9]$   

The maximum number of values Nick can remove is $\begin{array}{c}4\end{array}$.  There are two sets of choices with this result.  

Remove $1,2,3,4$ from $\class{ML__boldsymbol}{\boldsymbol{a}}$ with a sum of $10$.   
Remove $1,2,3$ from $\class{ML__boldsymbol}{\boldsymbol{a}}$ and $\boldsymbol{6}$ from $\boldsymbol{b}$ with a sum of $12$.   

Function Description 

Complete the twoStacks function in the editor below.  

twoStacks has the following parameters:
- int maxSum: the maximum allowed sum 

- int a[n]: the first stack 

- int b[m]: the second stack   

Returns 

- int: the maximum number of selections Nick can make   

Input Format

The first line contains an integer, $\mathrm{~g~}$ (the number of games). The $3\cdot g$ subsequent lines describe each game in the following format:

The first line contains three space-separated integers describing the respective values of $n$ (the number of integers in stack $\class{ML__boldsymbol}{\boldsymbol{a}}$), $m$ (the number of integers in stack $\boldsymbol{b}$), and $maxSum$ (the number that the sum of the integers removed from the two stacks cannot exceed).
The second line contains $n$ space-separated integers, the respective values of $a[i]$.
The third line contains $m$ space-separated integers, the respective values of $b[i]$.

Constraints

$1\leq g\leq50$
$1\leq n,m\leq10^5$
$0\leq a[i],b[i]\leq10^6$
$1\leq\textit{maxSum}\leq10^9$

Subtasks

$1\le n,m,\le100$ for $50\%$ of the maximum score.

Sample Input 0
1
5 4 10
4 2 4 6 1
2 1 8 5

Sample Output 0
4

Explanation 0

The two stacks initially look like this:

The image below depicts the integers Nick should choose to remove from the stacks. We print $\begin{array}{c}4\end{array}$ as our answer, because that is the maximum number of integers that can be removed from the two stacks without the sum exceeding $x=10$.

(There can be multiple ways to remove the integers from the stack, the image shows just one of them.)

Difficulty: MEDIUM
Tags: Data structures
Skill types: Data structures

Starter code:

Solutions:
import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	i = 0
	while i < len(a) and x >= a[i]:
		x -= a[i]
		i += 1
	ans = i
	j = 0
	for p in b:
		j += 1
		x -= p
		while x < 0 and i > 0:
			i -= 1
			x += a[i]
		if x >= 0:
			ans = max(ans, j + i)
	print(ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	(suma, sumb) = ([0], [0])
	cur = 0
	for i in range(n):
		cur += a[i]
		suma.append(cur)
	cur = 0
	for i in range(m):
		cur += b[i]
		sumb.append(cur)
	j = m
	ans = 0
	for i in range(n + 1):
		while j >= 0 and suma[i] + sumb[j] > x:
			j -= 1
		if j >= 0 and i + j > ans:
			ans = i + j
	print(ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	total = 0
	removedFromB = 0
	while removedFromB < m and total + b[removedFromB] <= x:
		total += b[removedFromB]
		removedFromB += 1
	answer = removedFromB
	for removedFromA in range(1, 1 + n):
		total += a[removedFromA - 1]
		while total > x and removedFromB > 0:
			removedFromB -= 1
			total -= b[removedFromB]
		if total <= x and answer < removedFromA + removedFromB:
			answer = removedFromA + removedFromB
	print(answer)


import sys

def get(a, b, x):
	sum = 0
	maxr = 0
	(a1, b1) = (0, 0)
	while a1 < len(a):
		if sum + a[a1] > x:
			break
		sum += a[a1]
		a1 += 1
	maxr = a1
	while True:
		if b1 == len(b):
			break
		if sum + b[b1] <= x:
			sum += b[b1]
			b1 += 1
			if a1 + b1 > maxr:
				maxr = a1 + b1
			continue
		if a1 == 0:
			break
		a1 -= 1
		sum -= a[a1]
	return maxr
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(get(a, b, x))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sumso = 0
	aindex = 0
	for i in range(n + 1):
		if aindex < n:
			if sumso + a[aindex] <= x:
				sumso += a[aindex]
				aindex += 1
	sumother = 0
	cindex = 0
	for j in range(m + 1):
		if cindex < m:
			if sumother + b[cindex] <= x:
				sumother += b[cindex]
				cindex += 1
	maxPossible = max(aindex, cindex)
	bindex = 0
	while bindex < m and aindex > -1:
		if sumso + b[bindex] <= x:
			sumso += b[bindex]
			bindex += 1
			maxPossible = max(maxPossible, aindex + bindex)
		else:
			if aindex > 0:
				sumso -= a[aindex - 1]
			aindex -= 1
	print(maxPossible)


games = int(input())
for g in range(games):
	(n, m, x) = [int(x) for x in input().split()]
	A = [int(x) for x in input().split()]
	B = [int(x) for x in input().split()]
	ans = 0
	cur_A_num = 0
	cur_A_sum = 0
	cur_B_num = len(B)
	cur_B_sum = sum(B)
	while cur_A_num <= len(A):
		while cur_A_sum + cur_B_sum > x and cur_B_num != 0:
			cur_B_num -= 1
			cur_B_sum -= B[cur_B_num]
		if cur_A_sum + cur_B_sum <= x:
			ans = max(ans, cur_A_num + cur_B_num)
		else:
			break
		if cur_A_num == len(A):
			break
		cur_A_sum += A[cur_A_num]
		cur_A_num += 1
	print(ans)


for _ in range(int(input())):
	(n, m, x) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	sums = []
	tmp = 0
	for i in a:
		tmp += i
		if tmp > x:
			break
		sums.append(tmp)
	index = len(sums) - 1
	ans = []
	tmp = 0
	bs = 0
	ans.append(index + 1)
	if index >= 0:
		for i in b:
			bs += 1
			tmp += i
			while index > 0 and sums[index] + tmp > x:
				index -= 1
			if sums[index] + tmp <= x:
				ans.append(index + 1 + bs)
			if tmp > x:
				break
	tmp = 0
	bs = 0
	for i in b:
		tmp += i
		if tmp <= x:
			bs += 1
		else:
			break
	ans.append(bs)
	print(max(ans))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	ai = 0
	bi = 0
	sum = 0
	while ai < len(a):
		if a[ai] + sum > x:
			break
		sum += a[ai]
		ai += 1
	ai -= 1
	mx = ai + 1
	while True:
		while True:
			if bi >= len(b):
				break
			if sum + b[bi] > x:
				break
			sum += b[bi]
			bi += 1
		mx = max(mx, ai + 1 + bi)
		if ai == -1 or bi >= len(b):
			break
		if ai != -1:
			sum -= a[ai]
			ai -= 1
	print(mx)


import sys

def TravelCS(CSA, CSB, MaxVal):
	Ind = [0, 0]
	n = [len(CSA), len(CSB)]
	while Ind[0] < n[0] - 1:
		if CSA[Ind[0] + 1] <= MaxVal:
			Ind[0] += 1
		else:
			break
	MaxLeft = MaxVal - CSA[Ind[0]]
	while Ind[1] < n[1] - 1:
		if CSB[Ind[1] + 1] <= MaxLeft:
			Ind[1] += 1
		else:
			break
	MaxCount = Ind[0] + Ind[1]
	for i1 in range(Ind[0]):
		Ind[0] -= 1
		MaxLeft = MaxVal - CSA[Ind[0]]
		while Ind[1] < n[1] - 1:
			if CSB[Ind[1] + 1] <= MaxLeft:
				Ind[1] += 1
			else:
				break
		if Ind[0] + Ind[1] > MaxCount:
			MaxCount = Ind[0] + Ind[1]
	return MaxCount
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	CSA = [0]
	CSB = [0]
	for i1 in a:
		CSA.append(CSA[-1] + i1)
	for i1 in b:
		CSB.append(CSB[-1] + i1)
	print(TravelCS(CSA, CSB, x))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	ground_sum = 0
	ground_count = 0
	while ground_sum <= x and ground_count < n:
		ground_sum += a[ground_count]
		ground_count += 1
	if ground_sum > x:
		ground_count -= 1
		ground_sum -= a[ground_count]
	start = 0
	end = ground_count
	while ground_sum <= x and start < m:
		ground_sum += b[start]
		start += 1
		ground_count += 1
	if ground_sum > x:
		start -= 1
		ground_sum -= b[start]
		ground_count -= 1
	count = ground_count
	while end > 0:
		end -= 1
		ground_sum -= a[end]
		count -= 1
		while ground_sum <= x and start < m:
			ground_sum += b[start]
			start += 1
			count += 1
		if ground_sum > x:
			start -= 1
			ground_sum -= b[start]
			count -= 1
		if count > ground_count:
			ground_count = count
	print(ground_count)


import sys

def game(a, b, x):
	result = 0
	j = len(b)
	sa = 0
	sb = sum(b)
	for i in range(len(a) + 1):
		if i > 0:
			sa += a[i - 1]
		if sa > x:
			return result
		while sa + sb > x:
			j -= 1
			sb -= b[j]
			if j == -1:
				return result
		result = max(result, i + j)
	return result
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(game(a, b, x))


import sys

def count_second(total, stack, size, x):
	count = 0
	for i in range(size):
		if total + stack[i] <= x:
			total += stack[i]
			count += 1
		else:
			return (total, count)
	return (total, count)

def less(total, x, total_sec, count_sec, b):
	while total + total_sec > x:
		count_sec -= 1
		total_sec -= b[count_sec]
	return (total_sec, count_sec)

def count_first(a, n, b, m, x):
	(total_sec, count_sec) = count_second(0, b, m, x)
	count_max = count_sec
	total = 0
	count = 0
	for i in range(n):
		if total + a[i] <= x:
			total += a[i]
			count += 1
			(total_sec, count_sec) = less(total, x, total_sec, count_sec, b)
			if count + count_sec > count_max:
				count_max = count + count_sec
		else:
			return count_max
	return count_max
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	if n < m:
		print(count_first(a, n, b, m, x))
	else:
		print(count_first(b, m, a, n, x))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	temp = 0
	indexA = 0
	count = 0
	while temp <= x and indexA < len(a):
		if temp + a[indexA] <= x:
			temp += a[indexA]
			count += 1
			indexA += 1
		else:
			break
	indexA -= 1
	indexB = 0
	while temp <= x and indexB < len(b):
		if temp + b[indexB] <= x:
			temp += b[indexB]
			count += 1
			indexB += 1
		else:
			break
	tempCount = count
	while temp <= x:
		if indexA >= 0:
			temp -= a[indexA]
			indexA -= 1
			tempCount -= 1
		while temp <= x and indexB < len(b):
			if temp + b[indexB] <= x:
				temp += b[indexB]
				tempCount += 1
				indexB += 1
			else:
				break
		count = max(count, tempCount)
		if indexA == -1:
			break
	print(count)


import sys

def bisearch(r, v):
	lo = 1
	hi = len(r) - 1
	if r[1] > v:
		return 0
	if r[hi] <= v:
		return hi
	while lo < hi - 1:
		mid = int((lo + hi) / 2)
		if r[mid] <= v:
			lo = mid
		else:
			hi = mid
	return lo
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a1 = [0]
	b1 = [0]
	c = 0
	for i in range(len(a)):
		a1.append(a1[-1] + a[i])
	for i in range(len(b)):
		b1.append(b1[-1] + b[i])
	c = 0
	for i in range(len(a1)):
		if a1[i] <= x:
			h = bisearch(b1, x - a1[i])
			if i + h > c:
				c = i + h
	print(c)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = [0] + list(map(int, input().strip().split(' ')))
	b = [0] + list(map(int, input().strip().split(' ')))
	asum = 0
	start = 0
	for d in a[1:]:
		if d + asum <= x:
			asum += d
			start += 1
		else:
			break
	best_ans = start
	bsum = 0
	ind = 1
	while start >= 0:
		while ind < len(b):
			if asum + bsum + b[ind] <= x:
				bsum += b[ind]
				ind += 1
			else:
				break
		ind -= 1
		bsum -= b[ind]
		new_ans = start + ind
		if new_ans > best_ans:
			best_ans = new_ans
		if ind == len(b) - 1:
			break
		asum -= a[start]
		start -= 1
	print(best_ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	amount = 0
	s = 0
	i = 0
	while i < len(a):
		if s + a[i] <= x:
			s += a[i]
			i += 1
			amount += 1
		else:
			i -= 1
			break
	if i == len(a):
		i -= 1
	j = 0
	mx = amount
	while j < len(b):
		if s + b[j] <= x:
			s += b[j]
			j += 1
			amount += 1
			if amount > mx:
				mx = amount
		elif i > -1:
			s -= a[i]
			i -= 1
			amount -= 1
		else:
			break
	print(mx)


import sys
from itertools import accumulate
import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	A = list(accumulate(a))
	B = list(accumulate(b))
	maxRemove = max(bisect.bisect_right(A, x), bisect.bisect_right(B, x))
	if n > m:
		for (i, val) in enumerate(B):
			if val <= x:
				maxRemove = max(maxRemove, 1 + i + bisect.bisect_right(A, x - val))
			else:
				break
	else:
		for (i, val) in enumerate(A):
			if val <= x:
				maxRemove = max(maxRemove, 1 + i + bisect.bisect_right(B, x - val))
	print(maxRemove)


import sys

def getscore(n, m, x, a, b):
	total = 0
	ai = 0
	bi = 0
	while True:
		if ai >= n:
			break
		if total + a[ai] > x:
			break
		total += a[ai]
		ai += 1
	while True:
		if bi >= m:
			break
		if total + b[bi] > x:
			break
		total += b[bi]
		bi += 1
	curscore = ai + bi
	maxscore = curscore
	while True:
		ai -= 1
		if ai < 0:
			return maxscore
		total -= a[ai]
		curscore -= 1
		while True:
			if bi >= m:
				return maxscore
			if total + b[bi] > x:
				break
			total += b[bi]
			bi += 1
			curscore += 1
			if curscore > maxscore:
				maxscore = curscore
	return maxscore
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	if x < 1:
		print(0)
		continue
	print(getscore(n, m, x, a, b))


import sys

def getSumArray(a, x):
	n = len(a)
	suma = [0]
	for i in range(n):
		si = suma[i] + a[i]
		if si > x:
			break
		suma.append(si)
	return suma
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	suma = getSumArray(a, x)
	sumb = getSumArray(b, x)
	n = len(suma)
	m = len(sumb)
	i = n - 1
	j = 0
	mx = 0
	while i >= 0 and j < m:
		if suma[i] + sumb[j] <= x:
			mx = max(mx, i + j)
			j += 1
		else:
			i -= 1
	print(mx)


import sys
from bisect import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	run_a = []
	acc = 0
	for ax in a:
		acc += ax
		run_a.append(acc)
	best = bisect(run_a, x)
	bc = 0
	acc_b = 0
	for bx in b:
		bc += 1
		acc_b += bx
		if acc_b > x:
			break
		ac = bisect(run_a, x - acc_b)
		best = max(best, bc + ac)
	print(best)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = map(int, input().strip().split(' '))
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	asum = [0]
	bsum = [0]
	for i in range(n):
		if a[i] + asum[-1] <= x:
			asum.append(a[i] + asum[-1])
		else:
			break
	for j in range(m):
		if b[j] + bsum[-1] <= x:
			bsum.append(b[j] + bsum[-1])
		else:
			break
	j = len(bsum) - 1
	count_max = j
	count = j
	for i in range(1, len(asum)):
		while asum[i] + bsum[j] > x:
			count -= 1
			j -= 1
		if asum[i] + bsum[j] <= x:
			count += 1
			if count > count_max:
				count_max = count
	print(count_max)


import sys
import bisect

def find(arr, mx):
	i = bisect.bisect_right(arr, mx)
	return i - 1

def solve(a, b, mx):
	sa = [0]
	sb = [0]
	sum = 0
	for x in a:
		sum = sum + x
		sa.append(sum)
	sum = 0
	for x in b:
		sum = sum + x
		sb.append(sum)
	best = 0
	for i in range(len(sa)):
		if sa[i] > mx:
			break
		j = find(sb, mx - sa[i])
		temp = i + j
		if temp > best:
			best = temp
	print(best)
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	solve(a, b, x)


import bisect
from itertools import accumulate
for _ in range(int(input())):
	(n, m, x) = [int(z) for z in input().split()]
	a = [int(z) for z in input().split()]
	b = [int(z) for z in input().split()]
	ap = [0] + list(accumulate(a))
	bp = [0] + list(accumulate(b))
	res = 0
	index = bisect.bisect_right(ap, x)
	for i in range(index):
		j = bisect.bisect_right(bp, x - ap[i]) - 1
		res = max(res, i + j)
	print(res)


import sys

def sumi(l, x):
	if l[0] > x:
		return []
	for i in range(1, len(l)):
		l[i] = l[i - 1] + l[i]
		if l[i] > x:
			l = l[:i]
			break
	return l

def maxremovedb(a, b, x):
	maxremoved = len(b)
	bi = 0
	for i in range(len(a) - 1, -1, -1):
		f = maxremovedf(b, bi, x - a[i])
		if i + 1 + f + 1 > maxremoved:
			maxremoved = i + f + 2
		if f == len(b) - 1:
			break
		bi = f + 1
	return maxremoved

def maxremovedf(l, li, x):
	if len(l) == li or l[li] > x:
		return li - 1
	for i in range(li, len(l)):
		if l[i] > x:
			return i - 1
	return len(l) - 1
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a = sumi(a, x)
	b = sumi(b, x)
	print(maxremovedb(a, b, x))


import sys, bisect

def debug(*args):
	x = 0

def buildSubSumDict(a, x):
	subSumDict = {}
	numSlots = 0
	subSum = 0
	for n in a:
		numSlots += 1
		subSum += n
		if subSum > x:
			break
		subSumDict[subSum] = numSlots
	return subSumDict

def findAmountSlotsInDictForLessThan(dct, dctSortedKeys, cmpSum):
	i = bisect.bisect_right(dctSortedKeys, cmpSum)
	if i:
		return dct[dctSortedKeys[i - 1]]
	return 0

def findHighestAmountSlots(subSumA, subSumB, x):
	highestAmountSlots = 0
	subSumBSortedKeys = sorted(subSumB.keys())
	for subSum in subSumA:
		amountSlotsA = subSumA[subSum]
		diff = x - subSum
		amountSlotsB = findAmountSlotsInDictForLessThan(subSumB, subSumBSortedKeys, diff)
		amountSlots = amountSlotsA + amountSlotsB
		debug('A has subsum %d for %d slots' % (subSum, amountSlotsA) + '- with diff of %d we can take %d slots of other dict,' % (diff, amountSlotsB) + 'for a total of %d slots' % amountSlots)
		if amountSlots > highestAmountSlots:
			highestAmountSlots = amountSlots
	return highestAmountSlots
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	subSumA = buildSubSumDict(a, x)
	subSumB = buildSubSumDict(b, x)
	debug('subSumA', subSumA)
	debug('subSumB', subSumB)
	print(max(findHighestAmountSlots(subSumA, subSumB, x), findHighestAmountSlots(subSumB, subSumA, x)))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	res = 0
	(l, r, curr) = (0, 0, 0)
	score = 0
	while l < len(a) and curr + a[l] <= x:
		curr += a[l]
		score += 1
		l += 1
	while r < len(b) and curr + b[r] <= x:
		curr += b[r]
		score += 1
		r += 1
	res = score
	while l - 1 >= 0:
		curr -= a[l - 1]
		score -= 1
		l -= 1
		while r < len(b) and curr + b[r] <= x:
			curr += b[r]
			score += 1
			r += 1
		res = max(res, score)
	print(res)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	A = list(map(int, input().strip().split(' ')))
	B = list(map(int, input().strip().split(' ')))
	Aind = 0
	summa = 0
	while Aind < n and summa + A[Aind] <= x:
		summa += A[Aind]
		Aind += 1
	Bind = 0
	while Bind < m and summa + B[Bind] <= x:
		summa += B[Bind]
		Bind += 1
	maxi = Aind + Bind
	while Bind < m and Aind > 0:
		summa -= A[Aind - 1]
		Aind -= 1
		while Bind < m and summa + B[Bind] <= x:
			summa += B[Bind]
			Bind += 1
		maxi = max(maxi, Aind + Bind)
	print(maxi)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	best_count = 0
	a_sum = {}
	a_tab = 0
	i = 0
	while i < n:
		a_tab += a[i]
		if a_tab > x:
			break
		a_sum[i + 1] = a_tab
		i += 1
	a_count = i
	best_count = a_count
	b_tab = 0
	j = 0
	while j < m:
		b_tab += b[j]
		if b_tab > x:
			break
		j += 1
		i = max(best_count - j, 0)
		while i < a_count:
			if b_tab + a_sum[i + 1] > x:
				a_count = i
				break
			i += 1
		count = i + j
		if count > best_count:
			best_count = count
	print(best_count)


g = int(input().strip())
for a0 in range(g):
	(n, m, x) = [int(x) for x in input().strip().split(' ')]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	total = 0
	a_pos = 0
	b_pos = 0
	while b_pos < len(b) and total + b[b_pos] <= x:
		total += b[b_pos]
		b_pos += 1
	while a_pos < len(a) and total + a[a_pos] <= x:
		total += a[a_pos]
		a_pos += 1
	moves_max = a_pos + b_pos
	while b_pos != 0 and a_pos != len(a):
		b_pos -= 1
		total -= b[b_pos]
		while a_pos < len(a) and total + a[a_pos] <= x:
			total += a[a_pos]
			a_pos += 1
		moves_max = max(a_pos + b_pos, moves_max)
	print(moves_max)


import sys

def game(a, b, sum):
	aDict = [0]
	for i in range(len(a)):
		if aDict[i] + a[i] > sum:
			break
		aDict.append(aDict[i] + a[i])
	bDict = [0]
	for i in range(len(b)):
		if bDict[i] + b[i] > sum:
			break
		bDict.append(bDict[i] + b[i])
	pA = 0
	pB = len(bDict) - 1
	max = pB
	while pA != len(aDict) or pB != 0:
		total = aDict[pA] + bDict[pB]
		if total <= sum and pA + pB > max:
			max = pA + pB
		if total > sum:
			if pB > 0:
				pB -= 1
			else:
				pA += 1
		elif pA < len(aDict) - 1:
			pA += 1
		else:
			break
	return max
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	moves = game(a, b, x)
	print(moves)


import sys

def search(bal, arr, n, st):
	i = st
	res = -1
	if n <= 0 or st >= n:
		return (res, bal)
	while arr[i] <= bal:
		res = i
		i += 1
		if i >= n:
			break
	if res > -1:
		pass
	return (res, bal)
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	prea = []
	preb = []
	flaga = 1
	flagb = 1
	for i in range(max(n, m)):
		if i == 0:
			if a[i] <= x or b[i] <= x:
				prea.append(a[i])
				preb.append(b[i])
				if prea[0] > x:
					flaga = 0
				if preb[0] > x:
					flagb = 0
			else:
				break
		else:
			if i < n and flaga == 1:
				prea.append(prea[i - 1] + a[i])
				if prea[i] > x:
					flaga = 0
			if i < m and flagb == 1:
				preb.append(preb[i - 1] + b[i])
				if preb[i] > x:
					flagb = 0
	if flaga == 0 and len(prea) > 1:
		del prea[-1]
	if flagb == 0 and len(preb) > 1:
		del preb[-1]
	asl = len(prea)
	bsl = len(preb)
	if max(asl, bsl) == 0:
		print(0)
		continue
	if asl == 1 and bsl == 1:
		print(1)
		continue
	host = []
	guest = []
	if asl == bsl:
		if prea[-1] < preb[-1]:
			host = prea
			guest = preb
		else:
			(host, guest) = (preb, prea)
	elif asl > bsl:
		(host, guest) = (prea, preb)
	else:
		(host, guest) = (preb, prea)
	maxnum = max(asl, bsl)
	guestMax = min(asl, bsl)
	balance = x - host[-1]
	hostc = maxnum
	guestc = 0
	if guestMax == 0:
		print(maxnum)
	else:
		(rem, balance) = search(balance, guest, guestMax, 0)
		maxnum += rem + 1
		guestc = rem + 1
		guesti = rem
		hosti = hostc - 1
		while hosti > 0 and guesti < guestMax:
			balanceUpdate = host[hosti] - host[hosti - 1]
			balance += int(balanceUpdate)
			hostc -= 1
			hosti -= 1
			(new, ba) = search(balance, guest, guestMax, guestc)
			if new >= 0:
				guestc = max(new + 1, guestc)
				guesti = guestc - 1
			if maxnum < hostc + guestc:
				maxnum = guestc + hostc
		print(maxnum)


def brute(a, b):
	ans = 0
	a = [0] + a
	b = [0] + b
	for i in range(len(a)):
		for j in range(len(b)):
			sa = sum(a[:i + 1])
			sb = sum(b[:j + 1])
			if sa + sb > x:
				break
			ans = max(ans, i + j)
	return ans

def solve(a, b):
	total = 0
	i = len(a)
	ans = i
	j = 1
	for i in range(len(a)):
		total += a[i]
		if total > x:
			ans = i
			break
	ans_total = x + 1
	total = sum(a[:i])
	while total <= x and i > 0 or j < len(b):
		if total < x:
			total += b[j - 1]
			j += 1
		elif total > x:
			total -= a[i - 1]
			i -= 1
		else:
			total -= a[i - 1]
			i -= 1
			total += b[j - 1]
			j += 1
		if i < 1 and total > x:
			break
		ans = max(ans, i + j - 2)
		ans_total = min(total, ans_total)
	return ans if ans_total <= x else ans - 1
for _ in range(int(input())):
	(n, m, x) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	(_sum_a, _sum_b) = (sum(a), sum(b))
	if _sum_a + _sum_b <= x:
		print(n + m)
	elif n <= 200 and m <= 200:
		print(brute(a, b))
	else:
		ans = solve(a, b) if _sum_a < _sum_b else solve(b, a)
		print(ans)


import sys
from bisect import bisect_right

def get_arr(arr, x):
	r = []
	r.append(0)
	sm = 0
	for el in arr:
		sm += el
		if sm > x:
			break
		r.append(sm)
	return r
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a = get_arr(a, x)
	b = get_arr(b, x)
	print(max((bisect_right(b, x - a[count_from_a]) - 1 + count_from_a for count_from_a in range(len(a)))))


for _ in range(int(input())):
	(n, m, x) = tuple((int(x) for x in input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	counter = 0
	accum = 0
	i1 = 0
	i2 = 0
	max_count = 0
	while i1 < n:
		accum += a[i1]
		if accum > x:
			accum -= a[i1]
			i1 -= 1
			break
		i1 += 1
		counter += 1
	if i1 == n:
		i1 -= 1
	max_count = counter
	while i2 < len(b) and accum <= x:
		accum += b[i2]
		i2 += 1
		counter += 1
		while accum > x and i1 >= 0:
			accum -= a[i1]
			i1 -= 1
			counter -= 1
		if accum > x:
			break
		max_count = max((max_count, counter))
	print(max_count)


import sys
import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	sa = [a[0]]
	for i in range(1, len(a)):
		sa.append(sa[-1] + a[i])
	b = list(map(int, input().strip().split(' ')))
	sb = [b[0]]
	for i in range(1, len(b)):
		sb.append(sb[-1] + b[i])
	getsa = 0
	getsaindex = -1
	allmaxcount = 0
	while True:
		thismaxcount = getsaindex + 1
		allowsb = x - getsa
		getsbindex = bisect.bisect_left(sb, allowsb + 0.5)
		thismaxcount += getsbindex
		allmaxcount = max(allmaxcount, thismaxcount)
		getsaindex += 1
		if getsaindex == len(sa):
			break
		if sa[getsaindex] > x:
			break
		getsa = sa[getsaindex]
	print(allmaxcount)


from itertools import accumulate
from bisect import bisect
res = []
for _ in range(int(input())):
	(n, m, x) = map(int, input().split())
	a = tuple(accumulate(map(int, input().split())))
	b = tuple(accumulate(map(int, input().split())))
	ae = bisect(a, x)
	be = bisect(b, x)
	t = 0
	if ae < be:
		a = (0,) + a
		for i in range(ae + 1):
			s = bisect(b, x - a[i], hi=be)
			t = max(t, i + s)
	else:
		b = (0,) + b
		for i in range(be + 1):
			s = bisect(a, x - b[i], hi=ae)
			t = max(t, i + s)
	res.append(t)
print(*res, sep='\n')


import sys
x = 0
a = []
b = []

def solve():
	r = 0
	(ai, bi) = (0, 0)
	s = x
	while ai < len(a) and a[ai] <= s:
		s -= a[ai]
		ai += 1
	while bi < len(b) and b[bi] <= s:
		s -= b[bi]
		bi += 1
	c = ai + bi
	while s >= 0 and ai > 0 and (bi < len(b)):
		s -= b[bi]
		bi += 1
		while ai > 0 and s < 0:
			ai -= 1
			s += a[ai]
		while bi < len(b) and b[bi] <= s:
			s -= b[bi]
			bi += 1
		if s >= 0 and ai + bi > c:
			c = ai + bi
	return c
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = map(int, input().strip().split())
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(solve())


import sys
from bisect import bisect_right
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sa = [a[0]] * n
	for i in range(1, n):
		sa[i] = sa[i - 1] + a[i]
	r = 0
	sb = 0
	for i in range(m + 1):
		if sb > x:
			break
		j = bisect_right(sa, x - sb)
		if j:
			r = max(r, i + j)
		else:
			r = max(r, i)
		if i < m:
			sb += b[i]
	print(r)


import sys

def find_take_limit_for_stack(sum_limit, stack, start_at=0, start_sum=0):
	sum_ = start_sum
	for i in range(start_at, len(stack)):
		sum_ += stack[i]
		if sum_ > sum_limit:
			sum_ -= stack[i]
			return (i, sum_)
	return (len(stack), sum_)

def find_take_limit_for_two_stacks(sum_limit, stack_a, stack_b):
	(start_count, start_sum) = find_take_limit_for_stack(sum_limit, stack_a)
	a_count = start_count
	b_count = 0
	a_sum = start_sum
	b_sum = 0
	best_a_count = a_count
	best_b_count = 0
	best_ab_sum = a_sum + b_sum
	while True:
		(b_count, b_sum) = find_take_limit_for_stack(sum_limit - a_sum, stack_b, b_count, b_sum)
		if a_count + b_count > best_a_count + best_b_count:
			best_a_count = a_count
			best_b_count = b_count
			best_ab_sum = a_sum + b_sum
		if a_count == 0 or b_count == len(stack_b):
			break
		a_sum -= stack_a[a_count - 1]
		a_count -= 1
	return (best_a_count, best_b_count, best_ab_sum)
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	max_sum = x
	stack_a = list(map(int, input().strip().split(' ')))
	stack_b = list(map(int, input().strip().split(' ')))
	result = find_take_limit_for_two_stacks(max_sum, stack_a, stack_b)
	print(result[0] + result[1])


import sys
from itertools import accumulate
from bisect import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	sa = [0] + list(accumulate(a))
	sb = [0] + list(accumulate(b))
	bi = 0
	while bi + 1 < len(b) + 1 and sb[bi + 1] <= x:
		bi += 1
	score = bi
	for ai in range(1, len(a) + 1):
		if sa[ai] > x:
			break
		while not sa[ai] + sb[bi] <= x:
			bi -= 1
		score = max(score, ai + bi)
	print(score)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	count = 0
	toplam = 0
	IsBreaked = 0
	for IndexA in range(len(a)):
		if a[IndexA] + toplam <= x:
			toplam += a[IndexA]
			count += 1
		else:
			IsBreaked = 1
			break
	if IsBreaked == 1:
		IndexA -= 1
	MaxCount = count
	Olumcul = 0
	for IndexB in range(len(b)):
		while True:
			if b[IndexB] + toplam <= x:
				toplam += b[IndexB]
				count += 1
				break
			elif IndexA == -1:
				Olumcul = 1
				break
			else:
				toplam -= a[IndexA]
				count -= 1
				IndexA -= 1
		if MaxCount < count:
			MaxCount = count
		if Olumcul == 1:
			break
	print(MaxCount)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	count = 0
	toplam = 0
	IsBreaked = 0
	for IndexA in range(len(a)):
		if a[IndexA] + toplam <= x:
			toplam += a[IndexA]
			count += 1
		else:
			IsBreaked = 1
			break
	if IsBreaked == 1:
		IndexA -= 1
	MaxCount = count
	Olumcul = 0
	for IndexB in range(len(b)):
		while True:
			if b[IndexB] + toplam <= x:
				toplam += b[IndexB]
				count += 1
				break
			elif IndexA == -1:
				Olumcul = 1
				break
			else:
				toplam -= a[IndexA]
				count -= 1
				IndexA -= 1
		if MaxCount < count:
			MaxCount = count
		if Olumcul == 1:
			break
	print(MaxCount)


from bisect import bisect
T = int(input())
for t in range(T):
	(n, m, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	aa = [0]
	for v in a:
		aa.append(aa[-1] + v)
	bb = [0]
	for v in b:
		bb.append(bb[-1] + v)
	r = [i + bisect(bb, x - v) - 1 for (i, v) in enumerate(aa) if v <= x]
	print(max(r))


import sys
gn = int(input().strip())
for _ in range(gn):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sa = [0]
	for i in a:
		sa.append(sa[-1] + i)
	sb = [0]
	for i in b:
		sb.append(sb[-1] + i)
	j = m
	ans = 0
	for i in range(n + 1):
		if sa[i] > x:
			break
		while sa[i] + sb[j] > x:
			j -= 1
		ans = max(ans, i + j)
	print(ans)


import sys

def decrement_b(b, x, b_total, b_count, a_total):
	while a_total + b_total > x:
		b_count -= 1
		b_total -= b[b_count]
	return (b_total, b_count)

def total_b(b, m, x):
	(b_total, b_count) = (0, 0)
	for i in range(m):
		if b_total + b[i] <= x:
			b_count += 1
			b_total += b[i]
		else:
			return (b_total, b_count)
	return (b_total, b_count)

def total_a(a, n, b, m, x):
	(a_total, a_count) = (0, 0)
	(b_total, b_count) = total_b(b, m, x)
	max_count = b_count
	for i in range(n):
		if a_total + a[i] <= x:
			a_count += 1
			a_total += a[i]
			(b_total, b_count) = decrement_b(b, x, b_total, b_count, a_total)
			if a_count + b_count > max_count:
				max_count = a_count + b_count
		else:
			return max_count
	return max_count
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(total_a(a, n, b, m, x))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	t = [0] * n
	t[0] = a[0]
	t1 = [0] * m
	t1[0] = b[0]
	w = [0] * (n + m + 1)
	for i in range(1, n):
		t[i] = t[i - 1] + a[i]
	for i in range(1, m):
		t1[i] = t1[i - 1] + b[i]
	(z, z1, m1, m2) = (0, 0, 0, 0)
	l = m - 1
	while t[m1] <= x:
		if m1 == n - 1:
			m1 += 1
			break
		m1 += 1
	while t1[m2] <= x:
		if m2 == m - 1:
			m2 += 1
			break
		m2 += 1
	while z < n and l >= 0:
		if t[z] + t1[l] <= x:
			if z + l + 2 > z1:
				z1 = z + l + 2
			z += 1
		else:
			l -= 1
	print(max(z1, m1, m2))


import sys
from itertools import accumulate
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a.insert(0, 0)
	b.insert(0, 0)
	a = list(accumulate(a))
	b = list(accumulate(b))
	it = len(b) - 1
	mx = 0
	for i in range(len(a)):
		while it >= 0 and b[it] + a[i] > x:
			it -= 1
		if it >= 0 and b[it] + a[i] <= x:
			mx = max(mx, it + i)
	print(mx)


def solve(A, B, cap):
	s1 = 0
	n = len(A)
	for (i, a) in enumerate(A):
		if s1 + a > cap:
			n = i
			break
		s1 += a
	s2 = 0
	m = len(B)
	for (i, b) in enumerate(B):
		if s1 + s2 + b > cap:
			m = i
			break
		s2 += b
	ans = n + m
	for i in range(n):
		s1 -= A[n - 1]
		n -= 1
		for j in range(m, len(B)):
			m = len(B)
			if s1 + s2 + B[j] > cap:
				m = j
				break
			s2 += B[j]
		if n + m > ans:
			ans = n + m
	return ans
g = int(input())
for _ in range(g):
	(n, m, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(solve(a, b, x))


import sys

def getMoves(a, b, _sum):
	moves = 0
	a_index = -1
	for (i, v) in enumerate(a):
		if v > _sum:
			break
		_sum -= v
		moves += 1
		a_index = i
	_max_moves = moves
	for (i, v) in enumerate(b):
		while v > _sum:
			if a_index == -1:
				return _max_moves
			_sum += a[a_index]
			a_index -= 1
			moves -= 1
		_sum -= v
		moves += 1
		_max_moves = max(_max_moves, moves)
	return _max_moves
	return _max_moves
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(getMoves(a, b, x))


import sys
import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	max_el = 0
	t = 0
	for i in range(n):
		t += a[i]
		a[i] = t
	t = 0
	for i in range(m):
		t += b[i]
		b[i] = t
	current = 0
	elements = 0
	i = 0
	while current <= x:
		elements = bisect.bisect_left(b, float(x - current + 0.5))
		if elements + i > max_el:
			max_el = elements + i
		if i >= n:
			break
		current = a[i]
		i += 1
	print(max_el)


import operator
from math import ceil
inf = float('inf')
read_ints = lambda : list(map(int, input().split()))

def accumulate(iterable, func=operator.add):
	it = iter(iterable)
	total = next(it)
	yield total
	for element in it:
		total = func(total, element)
		yield total

def lower_bound(L, value):
	l = 0
	r = len(L) - 1
	while l != r:
		mid = ceil((l + r) / 2)
		if L[mid] <= value:
			l = mid
		else:
			r = mid - 1
	return l

def solve(n, m, x, A, B):
	pA = [0] + list(accumulate(A))
	pB = [0] + list(accumulate(B))
	ans = -inf
	for (i, pa) in enumerate(pA):
		j = lower_bound(pB, x - pa)
		if pA[i] + pB[j] <= x:
			ans = max(ans, i + j)
	return ans
T = int(input())
for _ in range(T):
	(n, m, x) = read_ints()
	A = read_ints()
	B = read_ints()
	print(solve(n, m, x, A, B))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	ptr_a = 0
	ptr_b = 0
	cur_sum = 0
	while cur_sum <= x and ptr_a < n:
		cur_sum += a[ptr_a]
		ptr_a += 1
	if cur_sum > x:
		ptr_a -= 1
		cur_sum -= a[ptr_a]
	max_num = ptr_a
	cur_num = max_num
	while ptr_a >= 0 and ptr_b < m:
		while cur_sum <= x and ptr_b < m:
			cur_sum += b[ptr_b]
			ptr_b += 1
			cur_num += 1
		if cur_sum > x:
			ptr_b -= 1
			cur_sum -= b[ptr_b]
			cur_num -= 1
		if cur_num > max_num:
			max_num = cur_num
		ptr_a -= 1
		if ptr_a >= 0:
			cur_sum -= a[ptr_a]
			cur_num -= 1
	print(max_num)


import sys
from bisect import bisect_right
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	aa = [0] + a
	bb = [0] + b
	for i in range(1, len(a) + 1):
		aa[i] += aa[i - 1]
	for i in range(1, len(b) + 1):
		bb[i] += bb[i - 1]
	(al, bl) = (len(a), len(b))
	ans = 0
	for i in range(len(aa)):
		pos = bisect_right(bb, x - aa[i]) - 1
		if pos >= 0:
			ans = max(ans, i + pos)
	print(ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	(topa, topb, sum, c) = (-1, -1, 0, 0)
	while 1:
		if topa < len(a) - 1 and sum + a[topa + 1] <= x:
			topa = topa + 1
			sum = sum + a[topa]
		elif topb < len(b) - 1 and sum + b[topb + 1] <= x:
			topb = topb + 1
			sum = sum + b[topb]
		else:
			c = topa + topb + 2
			break
	while 1:
		if topa != -1:
			sum = sum - a[topa]
			topa = topa - 1
			while 1:
				if topb < len(b) - 1 and b[topb + 1] + sum <= x:
					topb = topb + 1
					sum = sum + b[topb]
				else:
					break
			if topa + topb + 2 > c:
				c = topa + topb + 2
		else:
			break
	print(c)


import bisect
for _ in range(int(input())):
	(n, m, k) = map(int, input().split())
	stack_a = list(map(int, input().split()))
	stack_b = list(map(int, input().split()))
	pre_stack_a = [0]
	pre_stack_b = [0]
	for i in range(len(stack_a)):
		pre_stack_a.append(pre_stack_a[i] + stack_a[i])
	for i in range(len(stack_b)):
		pre_stack_b.append(pre_stack_b[i] + stack_b[i])
	maxx = 0
	i = bisect.bisect_right(pre_stack_a, k) - 1
	for lp_var in range(1, i + 1):
		maxx = max(maxx, bisect.bisect_right(pre_stack_b, k - pre_stack_a[lp_var]) - 1 + lp_var)
	j = bisect.bisect_right(pre_stack_b, k) - 1
	for lp_var in range(1, j + 1):
		maxx = max(maxx, bisect.bisect_right(pre_stack_a, k - pre_stack_b[lp_var]) - 1 + lp_var)
	print(maxx)


import bisect as bst
for _ in range(int(input().strip())):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	l = 0
	l1 = []
	l2 = []
	for i in range(n):
		l1.append(l + a[i])
		l += a[i]
	l = 0
	for i in range(m):
		l2.append(l + b[i])
		l += b[i]
	ans1 = 0
	for i in range(m):
		y = x - l2[i]
		if y < 0:
			continue
		h = bst.bisect(l1, y)
		ans1 = max(ans1, i + 1 + h)
	ans2 = 0
	for i in range(n):
		y = x - l1[i]
		if y < 0:
			continue
		h = bst.bisect(l2, y)
		ans2 = max(ans2, i + 1 + h)
	print(max(ans1, ans2))


from bisect import bisect
g = int(input())
for _ in range(g):
	(n, m, x) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	apos = -1
	bpos = -1
	pre = 0
	for ix in range(n):
		A[ix] += pre
		if A[ix] > x:
			break
		pre = A[ix]
		apos = ix
	pre = 0
	for ix in range(m):
		B[ix] += pre
		if B[ix] > x:
			break
		pre = B[ix]
		bpos = ix
	res = max(bpos + 1, apos + 1)
	for i in range(apos, -1, -1):
		rm = bisect(B, x - A[i], hi=bpos + 1)
		if rm:
			res = max(res, rm + i + 1)
	print(res)


import sys
import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sa = []
	v = 0
	for i in a:
		v += i
		sa.append(v)
	sb = []
	v = 0
	for i in b:
		v += i
		sb.append(v)
	res = bisect.bisect_right(sb, x)
	for ai in range(0, len(sa)):
		if sa[ai] > x:
			break
		gb = x - sa[ai]
		bi = bisect.bisect_right(sb, gb)
		res = max(res, ai + bi + 1)
	print(res)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	suma = sum(a)
	sumb = sum(b)
	while suma > x:
		suma -= a.pop()
	top = suma
	count = len(a)
	Max = count
	j = len(a) - 1
	i = 0
	Allah = 1
	while i < len(b):
		while Allah > 0:
			if x - top >= b[i] and j != -1:
				count += 1
				top += b[i]
				break
			elif j != -1:
				top -= a[j]
				count -= 1
				j -= 1
				if j != -1:
					i -= 1
				break
			elif j == -1:
				break
		if Max < count:
			Max = count
		if j == -1:
			if x - top >= b[i]:
				count += 1
				top += b[i]
			else:
				break
		i += 1
	if Max < count:
		Max = count
	print(Max)


from itertools import accumulate
for _ in range(int(input())):
	(n, m, x) = (int(x) for x in input().split())
	(a, b) = ([0] + list(accumulate((int(x) for x in input().split()))) for _ in range(2))
	i = n
	j = 0
	res = 0
	while j <= m and i:
		while a[i] + b[j] > x and i:
			i -= 1
		while j <= m and a[i] + b[j] <= x:
			res = max(res, i + j)
			j += 1
	print(res)


g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	countA = 0
	countB = 0
	count = 0
	countA_dict = {}
	countA_dict[0] = 0
	tot = 0
	for i in range(0, len(a)):
		tot = tot + a[i]
		if tot > x:
			break
		countA_dict[i + 1] = tot
		countA += 1
	count = countA
	totB = 0
	countB = 0
	sumB = 0
	for i in range(countA, -1, -1):
		totA = countA_dict[i]
		k = countB
		while k < len(b):
			tempSumB = sumB + b[k]
			if tempSumB + totA > x:
				break
			else:
				sumB = tempSumB
				countB = countB + 1
				k = k + 1
		tempCount = i + countB
		if tempCount > count:
			count = tempCount
	print(count)


from collections import deque

def fetch_input():
	games = []
	for _ in range(int(input())):
		(n, m, x) = [int(x) for x in input().strip().split(' ')]
		(sum_A, sum_B) = (0, 0)
		A = deque([])
		B = deque([])
		A = deque((int(x) for x in input().strip().split(' ')))
		B = deque((int(x) for x in input().strip().split(' ')))
		games.append([n, m, x, A, B])
	return games

def max_possible_score(game):
	(size_A, size_B, x, A, B) = game
	count = 0
	sum_A = sum(A)
	sum_B = sum(B)
	while sum_A > x:
		sum_A -= A.pop()
		size_A -= 1
	while sum_B > x:
		sum_B -= B.pop()
		size_B -= 1
	while x > 0:
		while A and A[0] == 0:
			A.popleft()
			size_A -= 1
			count += 1
		while B and B[0] == 0:
			B.popleft()
			size_B -= 1
			count += 1
		if not A and (not B):
			break
		else:
			if size_A > size_B:
				popped = A.popleft()
				x -= popped
				sum_A -= popped
				size_A -= 1
			elif size_B > size_A:
				popped = B.popleft()
				x -= popped
				sum_B -= popped
				size_B -= 1
			elif A[-1] < B[-1]:
				popped = A.popleft()
				x -= popped
				sum_A -= popped
				size_A -= 1
			else:
				popped = B.popleft()
				x -= popped
				sum_B -= popped
				size_B -= 1
			while A and sum_A > x:
				sum_A -= A.pop()
				size_A -= 1
			while B and sum_B > x:
				sum_B -= B.pop()
				size_B -= 1
			count += 1
	while A and A[0] == 0:
		A.popleft()
		size_A -= 1
		count += 1
	while B and B[0] == 0:
		B.popleft()
		size_B -= 1
		count += 1
	return count
print('\n'.join([str(max_possible_score(game)) for game in fetch_input()]))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	alist = list(map(int, input().strip().split(' ')))
	blist = list(map(int, input().strip().split(' ')))
	asum = list()
	asumindex = -1
	bsum = list()
	bsumindex = -1
	if alist[0] <= x:
		asum.insert(0, alist[0])
		for a in range(1, len(alist)):
			asumindex = a
			asum.insert(a, asum[a - 1] + alist[a])
			if asum[a] > x:
				asum.pop()
				asumindex = a - 1
				break
	if blist[0] <= x:
		bsum.insert(0, blist[0])
		for b in range(1, len(blist)):
			bsumindex = b
			bsum.insert(b, bsum[b - 1] + blist[b])
			if bsum[b] > x:
				bsum.pop()
				bsumindex = b - 1
				break
	endlist = asum
	endindex = asumindex
	startlist = bsum
	startindex = 0
	if bsumindex > asumindex:
		endlist = bsum
		endindex = bsumindex
		startlist = asum
		startindex = 0
	counter = 0
	if endindex == -1:
		print(0)
	elif startindex == -1:
		print(endindex + 1)
	else:
		currentSolution = endindex + 1
		currentendindex = endindex
		currentstartindex = 0
		currentSip = 0
		for ei in range(len(endlist) - 1, 0, -1):
			for si in range(currentSip, len(startlist), 1):
				currentSip = si
				counter = counter + 1
				if endlist[ei] + startlist[si] <= x:
					if currentSolution <= ei + si + 2:
						currentSolution = ei + si + 2
						currentendindex = ei
						currentstartindex = si
				else:
					break
		print(currentSolution)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	alist = list(map(int, input().strip().split(' ')))
	blist = list(map(int, input().strip().split(' ')))
	asum = list()
	asumindex = -1
	bsum = list()
	bsumindex = -1
	if alist[0] <= x:
		asum.insert(0, alist[0])
		for a in range(1, len(alist)):
			asumindex = a
			asum.insert(a, asum[a - 1] + alist[a])
			if asum[a] > x:
				asum.pop()
				asumindex = a - 1
				break
	if blist[0] <= x:
		bsum.insert(0, blist[0])
		for b in range(1, len(blist)):
			bsumindex = b
			bsum.insert(b, bsum[b - 1] + blist[b])
			if bsum[b] > x:
				bsum.pop()
				bsumindex = b - 1
				break
	endlist = asum
	endindex = asumindex
	startlist = bsum
	startindex = 0
	if bsumindex > asumindex:
		endlist = bsum
		endindex = bsumindex
		startlist = asum
		startindex = 0
	counter = 0
	if endindex == -1:
		print(0)
	elif startindex == -1:
		print(endindex + 1)
	else:
		currentSolution = endindex + 1
		currentendindex = endindex
		currentstartindex = 0
		currentSip = 0
		for ei in range(len(endlist) - 1, 0, -1):
			for si in range(currentSip, len(startlist), 1):
				currentSip = si
				counter = counter + 1
				if endlist[ei] + startlist[si] <= x:
					if currentSolution <= ei + si + 2:
						currentSolution = ei + si + 2
						currentendindex = ei
						currentstartindex = si
				else:
					break
		print(currentSolution)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	val = 0
	posa = posb = 0
	storedM = 0
	while val <= x and posa < len(a):
		val += a[posa]
		posa += 1
	if val > x:
		posa -= 1
		val -= a[posa]
	posa -= 1
	storedM = val
	sta = posa
	stb = -1
	changes = True
	while posa >= 0 or posb < len(b):
		if posb >= len(b):
			break
		if not changes:
			break
		changes = False
		if val + b[posb] <= x:
			val += b[posb]
			if posa + posb > sta + stb:
				storedM = val
				sta = posa
				stb = posb
			posb += 1
			changes = True
		elif posa >= 0:
			val -= a[posa]
			posa -= 1
			changes = True
	if sta < 0:
		print(stb + 1)
	elif stb < 0:
		print(sta + 1)
	else:
		print(sta + stb + 2)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sumA = [0]
	sumB = [0]
	for i in a:
		sumA.append(sumA[-1] + i)
		if sumA[-1] > x:
			del sumA[-1]
			break
	for i in b:
		sumB.append(sumB[-1] + i)
		if sumB[-1] > x:
			del sumB[-1]
			break
	j = len(sumA) - 1
	i = 0
	while True:
		if i + 1 >= len(sumB):
			break
		if sumA[j] + sumB[i + 1] <= x:
			i = i + 1
		elif j - 1 < 0 or i + 1 >= len(sumB):
			break
		else:
			j = j - 1
			i = i + 1
	print(i + j)


def solve(A, B, x):
	(i, s) = (0, 0)
	while i < len(A) and s + A[i] <= x:
		s += A[i]
		i += 1
	j = 0
	while j < len(B) and s + B[j] <= x:
		s += B[j]
		j += 1
	best = i + j
	while j < len(B):
		s += B[j]
		j += 1
		while i > 0 and s > x:
			i -= 1
			s -= A[i]
		if s > x:
			break
		best = max(best, i + j)
	return best
g = int(input())
for _ in range(g):
	(n, m, x) = map(int, input().split())
	A = tuple(map(int, input().split()))
	B = tuple(map(int, input().split()))
	print(solve(A, B, x))


import sys

def addResults(result, currentResult, maxResult, index):
	addValue = 0
	for i in range(index, len(result)):
		if result[i] + currentResult > maxResult:
			break
		else:
			addValue += 1
			currentResult += result[i]
	return (addValue, currentResult)

def runIt(a, b, x):
	aResult = 0
	aResultArray = []
	bResult = 0
	bResultArray = []
	for item in a:
		if aResult + item > x:
			break
		else:
			aResult += item
			aResultArray.append(item)
	for item in b:
		if bResult + item > x:
			break
		else:
			bResult += item
			bResultArray.append(item)
	max = len(aResultArray)
	current = max
	index = 0
	if len(aResultArray) == 0:
		max = len(bResultArray)
	for i in range(len(aResultArray) - 1, -2, -1):
		added = addResults(bResultArray, aResult, x, index)
		current += added[0]
		index += added[0]
		aResult = added[1]
		if current > max:
			max = current
		if i > -1:
			aResult -= aResultArray[i]
			current -= 1
	print(max)
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	runIt(a, b, x)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	(sum, i, j, res) = (0, 0, 0, 0)
	while i < len(a) and sum + a[i] <= x:
		sum = sum + a[i]
		i = i + 1
	res = i
	while True:
		while j < len(b) and sum + b[j] <= x:
			sum = sum + b[j]
			j = j + 1
		res = i + j if i + j > res else res
		if i == 0:
			break
		else:
			i = i - 1
			sum = sum - a[i]
	print(res)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sa = 0
	sb = 0
	summa = 0
	while len(a) > sa and summa + a[sa] <= x:
		summa += a[sa]
		sa += 1
	maxNums = sa
	while sa >= 0:
		while len(b) > sb and summa + b[sb] <= x:
			summa += b[sb]
			sb += 1
		maxNums = max(maxNums, sa + sb)
		sa -= 1
		summa -= a[sa]
		if sb == len(b):
			break
	print(maxNums)


import sys
from itertools import *

def getClosest(data, value, index):
	while index + 1 < len(data) and data[index + 1] <= value:
		index += 1
	if index < len(data) and data[index] > value:
		return -1
	return index
g = int(input().strip())
for _ in range(g):
	(N, M, X) = map(int, input().split())
	A = tuple(accumulate(map(int, input().split())))
	B = tuple(accumulate(map(int, input().split())))
	if A[0] < B[0]:
		(A, B) = (B, A)
	iB = getClosest(B, X, 0)
	iA = getClosest(A, X - B[iB], 0)
	maxAB = iA + iB + 2
	for i in range(iB - 1, -2, -1):
		iA = getClosest(A, X - B[i] if i >= 0 else X, iA)
		maxAB = max(maxAB, iA + i + 2)
	print(maxAB)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sum = 0
	i = 0
	j = 0
	while i < n:
		if sum + a[i] <= x:
			sum = sum + a[i]
			i = i + 1
		else:
			break
	while j < m:
		if sum + b[j] <= x:
			sum = sum + b[j]
			j = j + 1
		else:
			break
	k = i + j
	if i <= n:
		i = i - 1
		while j < m and b[j] <= x:
			if sum + b[j] <= x:
				sum = sum + b[j]
				j = j + 1
			elif i >= 0:
				if k < i + j + 1:
					k = i + j + 1
				sum = sum - a[i]
				i = i - 1
			else:
				if k < i + j + 1:
					k = i + j + 1
				break
		i = i + 1
	if k < i + j:
		k = i + j
	print(k)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sum = 0
	i = 0
	while i < n and sum + a[i] <= x:
		sum += a[i]
		i = i + 1
	ans = i
	j = 0
	while j < m and i >= 0:
		sum += b[j]
		j = j + 1
		while sum > x and i > 0:
			sum -= a[i - 1]
			i = i - 1
		if sum <= x and ans < i + j:
			ans = i + j
	print(ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a = list(reversed(a))
	b = list(reversed(b))
	ca = []
	cb = []
	c = 1
	maxl = 0
	curl = 0
	curs = 0
	while curs <= x and len(a) > 0 and (c == 1):
		temp = a.pop()
		if curs + temp <= x:
			ca.append(temp)
			curs += temp
			curl += 1
		else:
			a.append(temp)
			c = 0
	maxl = curl
	c = 1
	while curs <= x and len(b) > 0 and (c == 1):
		temp = b.pop()
		if curs + temp <= x:
			cb.append(temp)
			curs += temp
			curl += 1
		else:
			b.append(temp)
			c = 0
	maxl = curl
	c = 1
	while len(ca) > 0:
		temp = ca.pop()
		curl = curl - 1
		curs = curs - temp
		while curs <= x and len(b) > 0 and (c == 1):
			temp1 = b.pop()
			if curs + temp1 <= x:
				cb.append(temp1)
				curs += temp1
				curl += 1
			else:
				b.append(temp1)
				c = 0
		c = 1
		if curl > maxl:
			maxl = curl
		else:
			curl = curl
	print(maxl)


import sys

def findway(a, b, x):
	(ende, erg, ca) = (0, 0, 1)
	try:
		while a[ende] <= x:
			a[ende + 1] += a[ende]
			ende += 1
	except:
		pass
	if ende + 1 < len(a):
		a = a[:ende]
	lena = len(a)
	erg = lena
	ende = 0
	try:
		while b[ende] <= x:
			if lena > 0:
				if b[ende] + a[lena - 1] > x:
					lena -= 1
				elif ende + 1 + lena > erg:
					erg = ende + 1 + lena
			b[ende + 1] += b[ende]
			ende += 1
	except:
		pass
	if ende + 1 < len(b):
		b = b[:ende]
	lenb = len(b)
	if erg == len(a) and erg > lenb and (a[-1] > x):
		erg -= 1
	if lenb > erg and b[-1] <= x:
		print(lenb)
	else:
		print(erg)
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	findway(a, b, x)


import sys
import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	res = 0
	max_num_from_a = max_num_from_b = 0
	summ = 0
	for value in a:
		summ += value
		if summ > x:
			break
		max_num_from_a += 1
	summ = 0
	for value in b:
		summ += value
		if summ > x:
			break
		max_num_from_b += 1
	if max_num_from_a * max_num_from_b == 0:
		res = max(max_num_from_b, max_num_from_a)
	else:
		for k in range(1, len(b)):
			b[k] += b[k - 1]
		summ = 0
		for k in range(max_num_from_a + 1):
			if k > 0:
				summ += a[k - 1]
			index = bisect.bisect_right(b, x - summ, lo=0, hi=max_num_from_b)
			res = max(res, k + index)
	print(res)


import sys
from bisect import bisect
g = int(input().strip())
for _ in range(g):
	(n, m, x) = map(int, input().strip().split(' '))
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sum_a = [0]
	sum_b = [0]
	for i in range(1, n + 1):
		sum_a.append(sum_a[i - 1] + a[i - 1])
		if sum_a[i] > x:
			break
	for j in range(1, m + 1):
		sum_b.append(sum_b[j - 1] + b[j - 1])
		if sum_b[j] > x:
			break
	res = 0
	for i in range(len(sum_a)):
		tmp = x - sum_a[i]
		pos = bisect(sum_b, tmp)
		count = i + pos - 1
		if sum_a[i] + sum_b[pos - 1] <= x and count > res:
			res = count
	print(res)


import sys

def findway(a, b, x):
	(ende, erg) = (0, 0)
	try:
		while a[ende] <= x:
			a[ende + 1] += a[ende]
			ende += 1
		a = a[:ende]
		if a[-1] > x:
			del a[-1]
	except:
		pass
	lena = len(a)
	ende = 0
	try:
		while b[ende] <= x:
			if lena > 0:
				if b[ende] + a[lena - 1] > x:
					lena -= 1
				elif ende + 1 + lena > erg:
					erg = ende + 1 + lena
			b[ende + 1] += b[ende]
			ende += 1
		b = b[:ende]
		if b[-1] > x:
			del b[-1]
	except:
		pass
	print(max(len(a), len(b), erg))
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	findway(a, b, x)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	(ende, erg) = (0, 0)
	try:
		while a[ende] <= x:
			a[ende + 1] += a[ende]
			ende += 1
		a = a[:ende]
		if a[-1] > x:
			del a[-1]
	except:
		pass
	lena = len(a)
	ende = 0
	try:
		while b[ende] <= x:
			if lena > 0:
				if b[ende] + a[lena - 1] > x:
					lena -= 1
				elif ende + 1 + lena > erg:
					erg = ende + 1 + lena
			b[ende + 1] += b[ende]
			ende += 1
		b = b[:ende]
		if b[-1] > x:
			del b[-1]
	except:
		pass
	print(max(len(a), len(b), erg))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a_ptr = 0
	b_ptr = 0
	curr = 0
	picks = 0
	maxval = 0
	while a_ptr < n:
		if curr + a[a_ptr] > x:
			break
		curr = curr + a[a_ptr]
		picks = picks + 1
		maxval = picks
		a_ptr = a_ptr + 1
	a_ptr = a_ptr - 1
	while b_ptr < m:
		if curr + b[b_ptr] > x:
			if a_ptr == -1:
				break
			curr = curr - a[a_ptr]
			picks = picks - 1
			a_ptr = a_ptr - 1
		else:
			curr = curr + b[b_ptr]
			picks = picks + 1
			b_ptr = b_ptr + 1
		if picks > maxval:
			maxval = picks
	print(maxval)


def findway(a, b, x):
	(ende, erg) = (0, 0)
	try:
		while a[ende] <= x:
			a[ende + 1] += a[ende]
			ende += 1
		a = a[:ende]
		if a[-1] > x:
			del a[-1]
	except:
		pass
	lena = len(a)
	ende = 0
	try:
		while b[ende] <= x:
			if lena > 0:
				if b[ende] + a[lena - 1] > x:
					lena -= 1
				elif ende + 1 + lena > erg:
					erg = ende + 1 + lena
			b[ende + 1] += b[ende]
			ende += 1
		b = b[:ende]
		if b[-1] > x:
			del b[-1]
	except:
		pass
	return max(len(a), len(b), erg)
import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(findway(a, b, x))


for _ in range(int(input().strip())):
	(n, m, x) = list(map(int, input().strip().split(' ')))
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	a_ptr = b_ptr = curr = picks = maxval = 0
	while a_ptr < n:
		if curr + a[a_ptr] > x:
			break
		curr += a[a_ptr]
		picks += 1
		maxval = picks
		a_ptr += 1
	a_ptr -= 1
	while b_ptr < m:
		if curr + b[b_ptr] > x:
			if a_ptr == -1:
				break
			curr -= a[a_ptr]
			picks -= 1
			a_ptr -= 1
		else:
			curr += b[b_ptr]
			picks += 1
			b_ptr += 1
		if picks > maxval:
			maxval = picks
	print(maxval)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	for i in range(1, m):
		b[i] += b[i - 1]
	ans = 0
	k = m - 1
	for i in range(n + 1):
		if i == 0:
			s = 0
		else:
			s += a[i - 1]
		if s > x:
			break
		while k >= 0 and s + b[k] > x:
			k -= 1
		if ans < i + k + 1:
			ans = i + k + 1
	print(ans)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	currentSum = 0
	maximumCount = 0
	maxIndexA = 0
	currentCount = 0
	j = 0
	for i in range(0, len(a)):
		if currentSum + a[i] <= x:
			maximumCount += 1
			currentCount += 1
			maxIndexA += 1
			currentSum += a[i]
		else:
			break
	if maxIndexA != 0:
		maxIndexA -= 1
		for i in range(maxIndexA, -1, -1):
			while j < len(b) and currentSum + b[j] <= x:
				currentCount += 1
				if currentCount > maximumCount:
					maximumCount = currentCount
				currentSum += b[j]
				j += 1
			currentSum -= a[i]
			currentCount -= 1
	while j < len(b) and currentSum + b[j] <= x:
		currentCount += 1
		if currentCount > maximumCount:
			maximumCount = currentCount
		currentSum += b[j]
		j += 1
	print(maximumCount)


games = int(input())
for iteration in range(games):
	(stack1, stack2, maxSum) = map(int, input().split())
	removed1 = list(map(int, input().split()))
	removed2 = list(map(int, input().split()))
	i = 0
	j = 0
	tempSum = 0
	answer = 0
	while i < stack1 and tempSum + removed1[i] <= maxSum:
		tempSum += removed1[i]
		i += 1
	answer = i
	while j < stack2 and i >= 0:
		tempSum += removed2[j]
		j += 1
		while tempSum > maxSum and i > 0:
			i -= 1
			tempSum -= removed1[i]
		if tempSum <= maxSum and i + j > answer:
			answer = i + j
	print(answer)


from bisect import bisect

def ans():
	for _ in range(int(input())):
		(n, m, x) = map(int, input().split())
		alist = tuple(map(int, input().split()))
		blist = tuple(map(int, input().split()))
		if n > m:
			(n, m, alist, blist) = (m, n, blist, alist)
		asum = [0] * (n + 1)
		bsum = [0] * (m + 1)
		for (i, v) in enumerate(alist, start=1):
			asum[i] = asum[i - 1] + v
		for (i, v) in enumerate(blist, start=1):
			bsum[i] = bsum[i - 1] + v
		i = j = res = 0
		while i < n and asum[i] <= x:
			j = bisect(bsum, x - asum[i]) - 1
			if j < 0:
				break
			i = bisect(asum, x - bsum[j]) - 1
			res = max(res, i + j)
			i += 1
		yield str(res)
print('\n'.join(ans()))


def bis(b, y, l, r):
	if b[l] > y:
		return l - 1
	while r - l > 1:
		m = (r + l) // 2
		if b[m] <= y:
			l = m
		else:
			r = m
	return l
g = int(input())
for gg in range(g):
	(n, m, x) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(1, n):
		a[i] += a[i - 1]
	for i in range(1, m):
		b[i] += b[i - 1]
	max_count = bis(b, x, 0, m) + 1
	j = m
	for i in range(n):
		sa = a[i]
		if sa > x:
			break
		y = x - sa
		j = bis(b, y, 0, j)
		max_count = max(max_count, i + j + 2)
		j += 1
	print(max_count)


import sys
from collections import deque
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	d = deque()
	(sumA, count, lastA) = (0, 0, 0)
	for el in a:
		if sumA + el <= x:
			d.append(el)
			sumA += el
			count += 1
		else:
			break
	maxx = count
	i = 0
	while i < m:
		el = b[i]
		if sumA + el <= x:
			sumA += el
			count += 1
			i += 1
			if count > maxx:
				maxx = count
		elif len(d) > 0:
			sumA -= d.pop()
			count -= 1
		else:
			break
	print(maxx)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	s = 0
	i = 0
	while i < n and s + a[i] <= x:
		s += a[i]
		i += 1
	smax = i
	j = 0
	while j < m and i >= 0:
		s += b[j]
		while s > x and i > 0:
			s -= a[i - 1]
			i -= 1
		j += 1
		if i + j > smax and s <= x:
			smax = i + j
	print(smax)


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	i = 0
	s = 0
	num = 0
	while i < len(a) and s + a[i] <= x:
		s += a[i]
		i += 1
	num = i
	j = 0
	while i >= 0 and j < len(b):
		s += b[j]
		j += 1
		while i > 0 and s > x:
			i -= 1
			s -= a[i]
		if s <= x:
			num = max(num, i + j)
	print(num)


x = input()
for _ in range(int(x)):
	(s1, s2, max_sum) = map(int, input().split())
	stack_1 = list(map(int, input().split()))
	stack_2 = list(map(int, input().split()))
	(i, j, temp_sum, ans) = (0, 0, 0, 0)
	while i < s1 and temp_sum + stack_1[i] <= max_sum:
		temp_sum += stack_1[i]
		i += 1
	ans = i
	while j < s2 and i >= 0:
		temp_sum += stack_2[j]
		j += 1
		while temp_sum > max_sum and i > 0:
			i -= 1
			temp_sum -= stack_1[i]
		if temp_sum <= max_sum and i + j > ans:
			ans = i + j
	print(ans)


import sys
from itertools import accumulate
from bisect import bisect_right
num_games = int(input().strip())
for _ in range(num_games):
	max_used = 0
	(n, m, x) = input().strip().split(' ')
	(num_stack_1, num_stack_2, tar_sum) = [int(n), int(m), int(x)]
	stack_1 = [0] + list(accumulate(map(int, input().strip().split(' '))))
	stack_2 = [0] + list(accumulate(map(int, input().strip().split(' '))))
	start_pos = bisect_right(stack_1, tar_sum) + 1
	for s1_pos in reversed(range(start_pos)):
		s1_val = stack_1[s1_pos - 1]
		s2_target = tar_sum - s1_val
		s2_pos = bisect_right(stack_2, s2_target)
		num_used = s1_pos + s2_pos - 2
		if num_used > max_used:
			max_used = num_used
	print(max_used)


import sys

class Node:

	def __init__(self, data):
		self.data = data
		self.next = None

class Stack:

	def __init__(self, data):
		self.top = Node(data)
		self.length = 0

	def __str__(self):
		output = ''
		node = self.top
		while node != None:
			output += str(node.data) + ' '
			node = node.next
		return output[:-1]

	def push(self, node):
		node.next = self.top
		self.top = node
		self.length += 1

	def pop(self):
		if self.top == None:
			return None
		node = self.top
		self.top = node.next
		node.next = None
		self.length -= 1
		return node
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	sta = Stack(0)
	stb = Stack(0)
	for i in a:
		j = i + sta.top.data
		if j > x:
			break
		sta.push(Node(j))
	k = 0
	mx = sta.length
	while sta.top != None:
		while k < m and stb.top.data + sta.top.data + b[k] <= x:
			stb.push(Node(stb.top.data + b[k]))
			k += 1
		mx = max(mx, sta.length + stb.length)
		sta.pop()
	print(mx)


import sys
from bisect import bisect
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	(cum_a, cum_b) = ([0], [0])
	for aa in a:
		cum_a.append(cum_a[-1] + aa)
	for bb in b:
		cum_b.append(cum_b[-1] + bb)
	n_max = 0
	for (ii, aa) in enumerate(cum_a):
		if aa > x:
			break
		jj = bisect(cum_b, x - aa)
		n_max = max(n_max, ii + jj - 1)
	print(n_max)


import sys

def xmaxRemoved(a, b, x, ai=0, bi=0, counter=0, total=0):
	if ai < len(a) and a[ai] + total <= x:
		maxA = maxRemoved(a, b, x, ai + 1, bi, counter + 1, total + a[ai])
	else:
		maxA = counter
	if bi < len(b) and b[bi] + total <= x:
		maxB = maxRemoved(a, b, x, ai, bi + 1, counter + 1, total + b[bi])
	else:
		maxB = counter
	return max(maxA, maxB)

def xxmaxRemoved(a, b, x):
	stack = [(0, 0, 0, 0)]
	highest = 0
	while len(stack) > 0:
		(ai, bi, counter, total) = stack.pop()
		if counter > highest:
			highest = counter
		if ai < len(a) and a[ai] + total <= x:
			stack.append((ai + 1, bi, counter + 1, total + a[ai]))
		if bi < len(b) and b[bi] + total <= x:
			stack.append((ai, bi + 1, counter + 1, total + b[bi]))
	return highest

def maxRemoved(a, b, x):
	total = 0
	ai = 0
	while ai < len(a) and a[ai] + total <= x:
		total += a[ai]
		ai += 1
	bi = 0
	answer = ai
	while bi < len(b) and ai >= 0:
		total += b[bi]
		bi += 1
		while total > x and ai > 0:
			ai -= 1
			total -= a[ai]
		if total <= x and ai + bi > answer:
			answer = ai + bi
	return answer
g = int(input().strip())
for _ in range(g):
	(_, _, x) = [int(s) for s in input().strip().split(' ')]
	a = [int(s) for s in input().strip().split(' ')]
	b = [int(s) for s in input().strip().split(' ')]
	print(maxRemoved(a, b, x))


import sys
import copy
from itertools import accumulate

def max_score(A, B, x):
	A = copy.copy(A)
	A.insert(0, 0)
	A_accu = list(accumulate(A))
	B_accu_rev = list(accumulate(B))[::-1]
	max_score = 0
	A_i = A_accu.pop()
	i = len(A_accu)
	B_j = 0
	j = 0
	while A_accu and B_accu_rev:
		if A_i + B_j <= x:
			max_score = max((max_score, i + j))
			B_j = B_accu_rev.pop()
			j += 1
		else:
			A_i = A_accu.pop()
			i = len(A_accu)
	if A_i + B_j <= x:
		max_score = max((max_score, i + j))
	while B_accu_rev:
		B_j = B_accu_rev.pop()
		j += 1
		if A_i + B_j <= x:
			max_score = max((max_score, i + j))
		else:
			break
	while A_accu:
		A_i = A_accu.pop()
		i = len(A_accu)
		if A_i + B_j <= x:
			max_score = max((max_score, i + j))
			break
	return max_score
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(max_score(a, b, x))


import sys
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))

	def sumArr(stack):
		sums = [0]
		sm = 0
		for val in stack:
			sm += val
			if sm > x:
				break
			sums.append(sm)
		return sums
	sumsA = sumArr(a)
	sumsB = sumArr(b)
	maxcounts = [0]
	ixB = len(sumsB) - 1
	for ixA in range(len(sumsA)):
		while ixB > 0 and sumsA[ixA] + sumsB[ixB] > x:
			ixB -= 1
		count = ixA + ixB
		maxcounts.append(count)
	print(max(maxcounts))


def gameOfTwoStacks():
	for _ in range(int(input())):
		(n, m, x) = map(int, input().split(' '))
		a = list(map(int, input().split(' ')))
		b = list(map(int, input().split(' ')))
		sa = [0]
		sb = [0]
		[sa.append(sa[-1] + i) for i in a]
		[sb.append(sb[-1] + i) for i in b]
		while sa[-1] > x:
			sa.pop()
		k = len(sa) - 1
		i = 0
		while sa:
			while i < len(sb) and sa[-1] + sb[i] <= x:
				i += 1
			k = max(k, len(sa) - 1 + i - 1)
			sa.pop()
		print(k)
gameOfTwoStacks()


def trim(s, x):
	ss = sum(s)
	while ss > x:
		ss -= s.pop()
	return (s, ss)
for _ in range(int(input())):
	(n1, n2, x) = map(int, input().split())
	(s1, ss) = trim(list(map(int, input().split())), x)
	s2 = list(map(int, input().split()))
	if ss + sum(s2) <= x:
		print(len(s1) + n2)
		continue
	i2 = 0
	while i2 < len(s2) and ss + s2[i2] <= x:
		ss += s2[i2]
		i2 += 1
	maxl = len(s1) + i2
	while s1:
		ss -= s1.pop()
		while i2 < len(s2) and ss + s2[i2] <= x:
			ss += s2[i2]
			i2 += 1
		maxl = max(maxl, len(s1) + i2)
	print(maxl)


import sys
from collections import deque

def game(a, b, x):
	aq = deque()
	bq = deque()
	a = deque(a)
	b = deque(b)
	total = 0
	current_length = 0
	max_length = 0
	while len(a) > 0 and total + a[0] <= x:
		add_to_q = a.popleft()
		total += add_to_q
		aq.append(add_to_q)
		current_length += 1
		max_length = max(max_length, current_length)
	while len(aq) >= 0:
		while len(b) > 0 and total + b[0] <= x:
			add_to_q = b.popleft()
			total += add_to_q
			bq.append(add_to_q)
			current_length += 1
			max_length = max(max_length, current_length)
		if len(aq) == 0:
			break
		else:
			sub_from_q = aq.pop()
			total -= sub_from_q
			current_length -= 1
			max_length = max(max_length, current_length)
	return max_length
g = int(input().strip())
for a0 in range(g):
	(n, m, x) = input().strip().split(' ')
	(n, m, x) = [int(n), int(m), int(x)]
	a = list(map(int, input().strip().split(' ')))
	b = list(map(int, input().strip().split(' ')))
	print(game(a, b, x))