//w,i will try
#include <bits/stdc++.h>
typedef unsigned long long ul;
using namespace std;
inline int gin() {
    int s=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') {
        s=(s<<3)+(s<<1)+(c^48);
        c=getchar();
    }
    return s*f;
}

const int N=3005;
int n,a[N],b[N];
ul c[N][N];
int sz,bk;

signed main() {
    freopen("match.in","r",stdin);
    freopen("match.out","w",stdout);
    gin();
    n=gin();
    for(int i=1;i<=n;i++) a[i]=gin();
    for(int i=1;i<=n;i++) b[i]=gin();
    for(int i=1;i<=n;i++) {
        int A=0,B=0;
        for(int j=i;j<=n;j++) {
            A=max(A,a[j]);
            B=max(B,b[j]);
            c[i][j]=(ul)A*B;
            // printf("%d %d %d %d\n",i,j,A,B);
        }
    }
    int q=gin();
    while(q--) {
        int l=gin(),r=gin();
        ul ans=0;
        for(int i=l;i<=r;i++) for(int j=i;j<=r;j++) {
            ans+=c[i][j];
            // printf("%d %d %llu\n",i,j,c[i][j]);
        }
        printf("%llu\n",ans);
    }
    return 0;
}