#include<bits/stdc++.h>
using namespace std;
inline int read(){
int ret=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-f;for(;isdigit(ch);ch=getchar())ret=(ret<<1)+(ret<<3)+(ch&15);return ret*f;
}
#define maxn 500005
typedef unsigned long long LL;int a[maxn],b[maxn];
void init(){
	int n=read();
	for(int i=1;i<=n;i++)a[i]=read();for(int i=1;i<=n;i++)b[i]=read();
	for(int q=read();q--;){int all=0;
		int l=read(),r=read();
		for(int i=l;i<=r;i++){
			int Maxa=-1,Maxb=-1;
			for(int j=i;j<=r;j++){
				Maxa=max(Maxa,a[j]),Maxb=max(Maxb,b[j]);
				all+=1ll*Maxa*Maxb;
			}
		}cout<<all<<endl;
	}
}
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);for(int t=1,id=read();t--;)init();
	return 0;
} 