#include<bits/stdc++.h>
using namespace std;
int t,n,a[100005],b[100005],q,c[3005][3005],d[3005][3005],aans,ansa,ansb;
inline int read()
{
	int f=1,s=0;
	char ch;
	ch=getchar();
	while(ch>'9'||ch<'0')
	{
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch<='9'&&ch>='0')
	{
		s=(s<<3)+(s<<1)+(ch^48);
		ch=getchar();
	}
	return s*f;
}
inline void write(int x)
{
	if(x<0)
	{
		putchar('-');
		x=-x;
	}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
int main()
{
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	t=read();
	n=read();
	for(int i=1;i<=n;i++)
	{
		a[i]=read();
	}
	for(int i=1;i<=n;i++)
	{
		b[i]=read();
	}
	q=read();
	while(q--)
	{
		int l,r;
		l=read();
		r=read();
		if(t<=10)
		{
			for(int k=1;k<=n;k++)
			{
				for(int i=1;i<=n-k+1;i++)
				{
					c[i][i+k-1]=max(a[i+k-1],c[i][i+k-2]);
					d[i][i+k-1]=max(b[i+k-1],d[i][i+k-2]);
					aans+=c[i][i+k-1]*d[i][i+k-1];
				}
			}
			write(aans);
			printf("\n");
		}
	}
	fclose(stdin);fclose(stdout);
	return 0;
}
