#include<bits/stdc++.h>
#define ll unsigned long long
#define ve vector<ll>
#define pa pair<ll,ll>
#define pb push_back
#define rd read()
using namespace std;
const ll mod=1e9+7,N=1000005;
ll n,m,q,a[N],b[N],ans;
inline long long read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c-48);
		c=getchar();
	}
	return f*x;
}
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	rd;n=rd;
	for(int i=1;i<=n;i++){
		a[i]=rd;
	}
	for(int i=1;i<=n;i++){
		b[i]=rd;
	}
	q=rd;
	while(q--){
		ll x=rd,y=rd,ans=0;
		for(int i=x;i<=y;i++){
			ll s1=0,s2=0;
			for(int j=i;j<=y;j++){
				s1=max(s1,a[j]);
				s2=max(s2,b[j]);
				ans+=s1*s2;
			}
		}
		printf("%lld\n",ans);
	}
}
