#include<bits/stdc++.h>
#define int long long
using namespace std;
int mod=18446744073709551616;
int t,n,q,l,r,maxa,maxb,a[250005],b[250005],ans=0;
signed main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin>>t;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) cin>>b[i];
	cin>>q;
	for(int temp=1;temp<=q;temp++){
		cin>>l>>r;
		for(int p=l;p<=r;p++)
		for(int q=p;q<=r;q++){
			for(int x=p;x<=q;x++){
				maxa=max(maxa,a[x]);
				maxb=max(maxb,b[x]);
			}
			ans+=maxa*maxb;
			maxa=-0x3f;maxb=-0x3f;
		}
		cout<<ans<<'\n';
	}		
	return 0;
}
