#include<bits/stdc++.h>
using namespace std;
long long tt,n,i,qq,q,l,r,x,y,sum,a[1000000],b[1000000],maxx,maxxx;
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	cin>>tt>>n;
	for (i=1;i<=n;i++) cin>>a[i];
	for (i=1;i<=n;i++) cin>>b[i];
	cin>>q;
	for (qq=1;qq<=q;qq++){
		cin>>l>>r;sum=0;
		for (x=l;x<=r;x++){
			for (y=x;y<=r;y++){
				maxx=0;maxxx=0;
				for (i=x;i<=y;i++){
					if (a[i]>maxx) maxx=a[i];
					if (b[i]>maxxx) maxxx=b[i];
				}
			sum=sum+maxx*maxxx;
			}
		}
	cout<<sum;
	}
	fclose(stdin);fclose(stdout);
	return 0;
}