#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N=250005;
int n;
ull a[N];
ull b[N];
int Q;

int lg2[N];
ull sta[N][30];
ull stb[N][30];

inline void init(){
//	scanf("%d",&n);
	cin>>n;
	for(register int i=1;i<=n;i++){
//		scanf("%lld",&a[i]);
		cin>>a[i];
		sta[i][0]=a[i];
	}
	for(register int i=1;i<=n;i++){
//		scanf("%lld",&b[i]);
		cin>>b[i];
		stb[i][0]=b[i];
	}
	return;
}


inline void pre(){
	
	for(register int i=2;i<=n;i++){
		lg2[i]=lg2[i/2]+1;
	}
	for(register int j=1;(1<<j)<=n;j++){
		for(register int i=1;i+(1<<j)-1<=n;i++){
			sta[i][j]=max(sta[i][j-1],sta[i+(1<<(j-1))][j-1]);
			stb[i][j]=max(stb[i][j-1],stb[i+(1<<(j-1))][j-1]);
		}
	}
	return;
}

inline ull query_a(int l,int r){
	int t=lg2[r-l+1];
	return max(sta[l][t],sta[r-(1<<t)+1][t]);
}

inline ull query_b(int l,int r){
	int t=lg2[r-l+1];
	return max(stb[l][t],stb[r-(1<<t)+1][t]);
}

inline void solve(int l,int r){
	ull sum=0;
	for(register int i=l;i<=r;i++){
		for(register int j=i;j<=r;j++){
			sum+=query_a(i,j)*query_b(i,j);
		}
	}
	cout<<sum<<endl;
	return;
}

int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int id;
//	scanf("%d",&id);
	cin>>id;
	init();
	pre();
//	scanf("%d",&Q);
	cin>>Q;
	while(Q--){
		int l,r;
//		scanf("%d%d",&l,&r);
		cin>>l>>r;
		solve(l,r);
	}
	return 0;
}
//please give me some score,CCF,I love you!
//Please!

