#include<bits/stdc++.h>
#define MOD 4611686018427387904
using namespace std;
long long T,n,a[250005],b[250005],Q,l,r;
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	cin>>T>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];
	cin>>Q;
	while(Q>0){
		cin>>l>>r;
		long long ans=0;
		for(int p=l;p<=r;p++){
			long long maa=-1e9,mab=-1e9;
			for(int q=p;q<=r;q++){
				maa=max(a[q],maa);
				mab=max(b[q],mab);
				ans+=maa*mab;
				ans%=MOD;
			}
		}cout<<ans<<endl;
		Q--;
	}
}
