#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline int read(){
	int w=1,s=0;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(isdigit(ch)){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return w*s;
}
const int N=250010;
const ll mod=0x7f7f7f7f7f7f7f7f;
int t,q,n;
int a[N],b[N];
struct tree{
	int l,r,w,maxx;
}tr[N<<2];
struct tree2{
	int l,r,w,maxx;
}tr2[N<<2];
void build(int k,int l,int r){
	tr[k].l=l,tr[k].r=r;
	if(l==r){
		tr[k].w=tr[k].maxx=a[l];
		return;
	}
	int mid=l+r>>1;
	build(k<<1,l,mid);
	build(k<<1|1,mid+1,r);
	tr[k].maxx=max(tr[k<<1].maxx,tr[k<<1|1].maxx);
}
void build2(int k,int l,int r){
	tr2[k].l=l,tr2[k].r=r;
	if(l==r){
		tr2[k].w=tr2[k].maxx=b[l];
		return;
	}
	int mid=l+r>>1;
	build2(k<<1,l,mid);
	build2(k<<1|1,mid+1,r);
	tr2[k].maxx=max(tr2[k<<1].maxx,tr2[k<<1|1].maxx);
}
ll query(int k,int l,int r,int x,int y){
	ll ans=0;
	if(x<=l&&r<=y) return tr[k].maxx;
	int mid=l+r>>1;
	if(x<=mid) ans=max(ans,query(k<<1,l,mid,x,y));
	if(y>mid) ans=max(ans,query(k<<1|1,mid+1,r,x,y));
	return ans;
}
ll query2(int k,int l,int r,int x,int y){
	ll ans=0;
	if(x<=l&&r<=y) return tr2[k].maxx;
	int mid=l+r>>1;
	if(x<=mid) ans=max(ans,query2(k<<1,l,mid,x,y));
	if(y>mid) ans=max(ans,query2(k<<1|1,mid+1,r,x,y));
	return ans;
}
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	t=read(),n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<=n;i++) b[i]=read();
	//ll mod=quick_power(2,64);
	build(1,1,n),build2(1,1,n);
	q=read();
	while(q--){
		int x,y;
		x=read(),y=read();
		ll ans=0;
		for(int p=x;p<=y;p++)
			for(int q=p;q<=y;q++)
				ans=(ans+query(1,1,n,p,q)*query2(1,1,n,p,q))%mod;
		printf("%lld\n",ans%mod);
	}
	return 0;
}
/*
0 2
2 1
1 2
1
1 2
*/
