#include<cstdio>
#include<algorithm>
#define maxn 1000000+10
using namespace std;
int s[maxn],p[maxn],h1[maxn],h2[maxn],n;
int solve()
{
int cnt=0,tmp1=s[n-1],tmp2=s[0];
for(int i=n-1; i>=0; i--)
{
if(tmp1<p[i])tmp1=p[i];
if(tmp1>s[i])tmp1=s[i];
h1[i]=tmp1;
}
for(int i=0; i<n; i++)
{
if(tmp2<p[i])tmp2=p[i];
if(tmp2>s[i])tmp2=s[i];
h2[i]=tmp2;
}
for(int i=0; i<n; i++)
{
h1[i]=min(h1[i],h2[i]);
cnt+=h1[i]-p[i];
}
return cnt;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&p[i]);
for(int i=0; i<n; i++)
scanf("%d",&s[i]);
printf("%d\n",solve());
}
return 0;
}
左右各扫一遍,求出满足条件的h