#include<bits/stdc++.h>
using namespace std;
int id,n,q;
namespace solve1{
	typedef unsigned long long ull;
	int const N=35;
	int a[N],b[N];
	void solve8pts(){
		for(int i=1;i<=n;i++)
			cin>>a[i];
		for(int i=1;i<=n;i++)
			cin>>b[i];
		for(cin>>q;q--;){
			int l,r;cin>>l>>r;
			ull res=0;
			for(int p=l;p<=r;p++)
				for(int q=p;q<=r;q++){
					int ma=0,mb=0;
					for(int i=p;i<=q;i++)
						ma=max(ma,a[i]),
						mb=max(mb,b[i]);
					res+=1llu*ma*mb;
				}
			cout<<res<<"\n";
		}
	}
}
int main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	ios::sync_with_stdio(0);
	cin>>id>>n;
	if(n<=30)
		solve1::solve8pts();
}
