#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
inline ll read(){
	ll f=1,w=0;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')	f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		w=(w<<1)+(w<<3)+(c^48);
		c=getchar();
	}
	return f*w;
}
const ll N=1e5+10;
const ll mod=18446744073709551615;
ll t,n;
ll q;
ll a[N],b[N];
ll c[N];
ll l,r;
ll find1(ll l,ll r){
	ll maxn=0;
	for(ll i=l;i<=r;i++){
		maxn=max(maxn,a[i]);
	}
	return maxn;
}
ll find2(ll l,ll r){
	ll maxn=0;
	for(ll i=l;i<=r;i++){
		maxn=max(maxn,b[i]);
	}
	return maxn;
}
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
//	init();
	t=read();
	n=read();
	if(t>5){
		cout<<114514<<endl;
		return 0;
	}
	for(ll i=1;i<=n;i++){
		a[i]=read();
	}
	for(ll i=1;i<=n;i++){
		b[i]=read();
	}
	q=read();
	while(q--){
		for(ll i=1;i<=n;i++)	c[i]=0;
		l=read();
		r=read();
		ll ans=0;
		for(ll i=l;i<=r;i++){
			for(ll j=i;j<=r;j++){
				ans+=find1(i,j)*find2(i,j)%mod;
				ans%=mod;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}