#include<bits/stdc++.h>
using namespace std;

int n,l,r,a[250001],b[250001];
int pa[1000001],pb[1000001];
unsigned long long ans;

void buildA(int s,int t,int p)
{
	if(s==t)
	{
		pa[p]=a[s];
		return;
	}
	int m=s+t>>1;
	buildA(s,m,p*2);
	buildA(m+1,t,p*2+1);
}

void buildB(int s,int t,int p)
{
	if(s==t)
	{
		pb[p]=b[s];
		return;
	}
	int m=s+t>>1;
	buildB(s,m,p*2);
	buildB(m+1,t,p*2+1);
}

int findA(int s,int t,int l,int r,int p)
{
	if(s==t)
		return pa[p];
	int m=s+t>>1,as1,as2;
	as1=as2=0;
	if(l<=m)
		as1=findA(s,m,l,r,p*2);
	if(m<r)
		as2=findA(m+1,t,l,r,p*2+1);
	return max(as1,as2);
}

int findB(int s,int t,int l,int r,int p)
{
	if(s==t)
		return pb[p];
	int m=s+t>>1,as1,as2;
	as1=as2=0;
	if(l<=m)
		as1=findB(s,m,l,r,p*2);
	if(m<r)
		as2=findB(m+1,t,l,r,p*2+1);
	return max(as1,as2);
}
int main()
{
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	int T,Q;
	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);
	if((T<=9 && T>=3) || (T>=14 && T<=17))
	{
		for(int I=1;I<=Q;I++)
		{
			ans=0;
			scanf("%d%d",&l,&r);
			for(int q=l;q<=r;q++)
				ans+=(unsigned long long)(q)*(unsigned long long)(q)*(unsigned long long)(q-l+1);
			printf("%u\n",ans);
		}
		return 0;
	}
	buildA(1,n,1);
	buildB(1,n,1);
	for(int I=1;I<=Q;I++)
	{
		ans=0;
		scanf("%d%d",&l,&r);
		for(int p=l;p<=r;p++)
			for(int q=p;q<=r;q++)
				ans+=(unsigned long long)(findA(1,n,p,q,1))*(unsigned long long)(findB(1,n,p,q,1));
		printf("%u\n",ans);
	}
	return 0;
}