#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
	long long s=0,k=1;
	char r=getchar();
	while(!isdigit(r))
	{
		k=(r=='-')?-1:1;
		r=getchar();
	}
	while(isdigit(r))
	{
		s=s*10+(r-'0');
		r=getchar();
	}
	return s*k;
}
#define d read()
#define ll long long
#define Maxn 10010
#define Size 250010
#define pb push_back
#define mp make_pair
#define fi first
#define se second
unsigned ll a[Size],b[Size];
ll sta[Size][20],stb[Size][20];
ll log_2[Size];
void pre(ll n)
{
	for(int i=1;i<=n;i++)
		sta[i][0]=a[i],stb[i][0]=b[i];
	for(int j=1,k=1;k*2<=n;k<<=1,j++)
		for(int i=1;i+k<=n;i++)
		{
			if(a[sta[i][j-1]]>a[sta[i+k][j-1]]) sta[i][j]=sta[i][j-1];
			else sta[i][j]=sta[i+k][j-1];
			if(b[stb[i][j-1]]>b[stb[i+k][j-1]]) stb[i][j]=stb[i][j-1];
			else stb[i][j]=stb[i+k][j-1];
		}
}
unsigned ll again(ll l,ll r,ll x)
{
	if(l>r||l>x||r<x) return 0;
	if(l==r)
	{
//		printf("again:%lld %lld %llu\n",l,r,b[l]);
		return b[l];
	}
	unsigned ll sum=0;
	ll len=1,j=0,pos;
	while(len<=(r-l+1)) len<<=1,j++;
	len>>=1,j--;
	if(b[stb[l][j]]>b[stb[r-len+1][j]]) pos=stb[l][j];
	else pos=stb[r-len+1][j];
	sum=again(l,pos-1,x)+again(pos+1,r,x);
	if(pos==x) sum=sum+(unsigned ll)(pos-l+1)*(r-pos+1)*b[pos];
	else if(pos<x) sum=sum+(unsigned ll)(pos-l+1)*(r-x+1)*b[pos];
	else sum=sum+(unsigned ll)(x-l+1)*(r-pos+1)*b[pos];
//	printf("again:%lld %lld %lld %llu\n",l,r,x,sum);
	return sum;
}
unsigned ll ques(ll l,ll r)
{
//	cerr<<l<<" "<<r<<endl;
	if(l>r) return 0;
	if(l==r)
	{
//		printf("ques:%lld %lld %llu\n",l,r,a[l]*b[l]);
		return a[l]*b[l];
	}
	unsigned ll sum=0;
	ll len=1,j=0,pos=0;
	while(len<=(r-l+1)) len<<=1,j++;
	len>>=1,j--;
	if(a[sta[l][j]]>a[sta[r-len+1][j]]) pos=sta[l][j];
	else pos=sta[r-len+1][j];
	sum=ques(l,pos-1);
	sum+=ques(pos+1,r);
	sum+=a[pos]*again(l,r,pos);
//	printf("ques:%lld %lld %llu\n",l,r,sum);
	return sum;
}
int main()
{
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);
	d;
	ll n=d;
	for(int i=1;i<=n;i++) a[i]=d;
	for(int i=1;i<=n;i++) b[i]=d;
	pre(n);
	ll q=d;
	while(q--)
	{
		ll l=d,r=d;
		printf("%llu\n",ques(l,r));
	}
}
