#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<vector>
using namespace std;
long long stk[1000050],tp=0;
long long a[1000050],b[1000050],m;
long long cnta,cntb;
long long ans=0;
bool fuck=false;
pair<long long,long long>lst;
void cmp(long long p1,long long p2){
long long r=min(a[p1],b[p2]);
a[p1]-=r;b[p2]-=r;
if(r>0)lst=make_pair(p1,p2);
if(r>0&&p1+p2>=m)fuck=true;
if(b[p2]>0)stk[++tp]=p2;
ans+=r;
}
long long main(){
long long t;cin>>t;
for(long long T=1;T<=t;T++){
tp=0;fuck=false;
cin>>m;
cnta=cntb=0;ans=0;
long long mxa=0,mxb=0;
for(long long i=0;i<m;i++){
cin>>a[i];cnta+=a[i];
// if(T==16)cout<<a[i]<<" ";
if(a[i]){
mxa=i;
}
}
for(long long i=0;i<m;i++){
cin>>b[i];cntb+=b[i];
// if(T==16)cout<<b[i]<<" ";
if(b[i]){
mxb=i;
}
}
if(mxa+mxb<m){
cout<<0<<endl;
continue;
}
cnta++;cntb++;
if(cnta>cntb){b[0]+=cnta-cntb;}else{a[0]+=cntb-cnta;}
for(long long i=0;i<m;i++){
stk[++tp]=m-i-1;
while(a[i]){
if(tp==0)break;
else{tp--;cmp(i,stk[tp+1]);}
}
}
// cout<<lst.first<<","<<lst.second<<endl;
for(long long i=0;i<m;i++){
if(b[i]&&i+lst.first>=m){
fuck=1;
// cout<<"A";
}
if(a[i]&&mxb+i>=m){
fuck=1;
// cout<<"B";
}
}
for(long long i=lst.first+1;i<m;i++){
if(a[i]){
fuck=1;
// cout<<"C";
}
}
if(fuck){
cout<<ans<<endl;
}else{
cout<<ans-1<<endl;
}
}
}