#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
int read()
{
    int x=0;bool f=0;char c=getchar();
    for(;!isdigit(c);c=getchar())f^=!(c-45);
    for(;isdigit(c);c=getchar())x=x*10+c-48;
    if(f)x=-x;return x;
}
int ti,n;
int a[250050],b[250050],va[250050],vb[250050];
struct tree
{
    struct node
    {
        int l,r,ans;
    }t[250050*4];
    void build(int x,int l,int r,int *a) 
    {
        t[x].l=l,t[x].r=r;
        if(l==r){t[x].ans=a[l];return ;}
        int mid=l+r>>1;
        build(x<<1,l,mid,a),build(x<<1|1,mid+1,r,a);
        t[x].ans=max(t[x<<1].ans,t[x<<1|1].ans);
    }
    int query(int x,int l,int r)
    {
        if(t[x].l>r||l>t[x].r)return 0;
        if(l<=t[x].l&&t[x].r<=r)return t[x].ans;
        return max(query(x<<1,l,r),query(x<<1|1,l,r));
    }
}A,B;
signed main()
{
    freopen("match.in","r",stdin);
    freopen("match.out","w",stdout);
    ti=read(),n=read();
    for(int i=1;i<=n;i++)a[i]=read(),va[a[i]]=i;
    for(int i=1;i<=n;i++)b[i]=read(),vb[b[i]]=i;
    A.build(1,1,n,a);B.build(1,1,n,b);
    int q=read();
    for(int i=1;i<=q;i++)
    {
        int l=read(),r=read();
        unsigned long long ans=0;
        for(int j=l;j<=r;j++)
            for(int k=j;k<=r;k++)
            {
                ans=(ans+(ull)A.query(1,j,k)*B.query(1,j,k));
            }
        cout<<ans<<"\n";
        /*while(l<=r)
        {
            int aa=A.query(1,l,r),bb=B.query(1,l,r);
            int tl=va[aa],tr=vb[bb];
            if(tl<=tr)
            {
                ans=ans+(ull)(tl-l+1)*(r-tr+1)*aa*bb;
                int R=tr-1;
                while(tl<=R)
                {
                    int bc=B.query(1,tl,R);
                    int RR=vb[bc];
                    ans=ans+(ull)(tl-l+1)*(R-RR+1)*aa*bc;
                    R=RR-1;
                }
                int L=tl+1;
                while(L<=tr)
                {
                    int ac=A.query(1,L,tr);
                    int LL=va[ac];
                    ans=ans+(ull)(LL-L+1)*(r-tr+1)*ac*bb;
                    L=LL+1;
                }
            }
            else
            {
                swap(tl,tr);
                ans=ans+(ull)(tl-l+1)*(r-tr+1)*aa*bb;
                int R=tr-1;
                while(tl<=R)
                {
                    int ac=A.query(1,tl,R);
                    int RR=va[ac];
                    ans=ans+(ull)(tl-l+1)*(R-RR+1)*bb*ac;
                    R=RR-1;
                }
                int L=tl+1;
                while(L<=tr)
                {
                    int bc=B.query(1,L,tr);
                    int LL=vb[bc];
                    ans=ans+(ull)(LL-L+1)*(r-tr+1)*aa*bc;
                    L=LL+1;
                }
            }
            l=tl+1,r=tr-1;
        }
        cout<<ans<<"\n";*/
    }
}