#include<bits/stdc++.h>
using namespace std;
int i,j,n,m,k,l,a[300000],b[300000],ma[300000],mb[300000],ans;
inline int read()
{
	bool f=0;int x=0;char c=getchar();
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if (f) x=-x;return x;
}
int main()
{
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	int T=read();n=read();
	if (T<=3000)
	{
		for (int i=1;i<=n;i++) a[i]=read(),ma[a[i]]=i;
		for (int i=1;i<=n;i++) b[i]=read(),mb[b[i]]=i;
		int Q=read();
		while(Q--)
		{
			int l=read(),r=read();ans=0;
			for (int i=l;i<=r;i++)
			  for (int j=i;j<=r;j++)
			    {
			    	int x=n,y=n;
			    	while(ma[x]<i||ma[x]>j) x-=1;
			    	while(mb[y]<i||mb[y]>j) y-=1;
			    	ans=ans+x*y;
				}
			cout<<ans<<endl;
		}
	}
	return 0;
}
