#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-48;
		c=getchar();
	}
	return x*f;
}
const int N=5e5+5,mod=1e9+7l;
int n,m,i,j,k,l,o,p,x,y,T,r,a[N],b[N],ans;
vector<int> g[N];
signed main(){
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
    T=read();n=read();
    for(i=1;i<=n;i++)a[i]=read();
    for(i=1;i<=n;i++)b[i]=read();
    T=read();
    while(T--){
    	l=read();r=read();ans=0;
    	for(i=l;i<=r;i++)
    	 for(j=i;j<=r;j++){
    	 	int maxx=0;
    	 	for(k=i;k<=j;k++)
    	 	  maxx=max(maxx,a[k]);
    	 	int M=0;
    	 	for(k=i;k<=j;k++)
    	 	  M=max(M,b[k]);
    	 	ans+=maxx*M;
		 }
		 cout<<ans<<'\n';
	}
	return 0;
}