#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
const int M=2e5+5e4+5;
const ull MOD=LLONG_MAX+1;
int T,n;
int a[M],b[M];
int Q;
int l,r;
int maxa,maxb;
ull ans,sum;
int main()
{
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	scanf("%d%d",&T,&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&b[i]);
	}
	scanf("%d",&Q);
	while(Q--)
	{
		ans=0;
		scanf("%d%d",&l,&r);
		for(int p=l;p<=r;++p)
		{
			for(int q=p;q<=r;++q)
			{
				maxa=-1,maxb=-1;
				
				for(int k=p;k<=q;++k)
				{
					if(maxa<a[k]) maxa=a[k];
					if(maxb<b[k]) maxb=b[k];
				}
				sum=maxa*maxb%MOD;
				ans+=sum;
				ans%=MOD;
			}
		}
		printf("%llu\n",ans%MOD);
	}
	return 0;
}

