#include<bits/stdc++.h>
#define poly vector<int>
#define ll unsigned long long 
#define pa pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define IOS ios::sync_with_stdio(false)
#define N 3005
#define int ll
using namespace std;
int n,q,a[N],b[N],f[N][N];
void BellaKira()
{
    cin>>n;
    for (int i=1;i<=n;i++) cin>>a[i];
    for (int i=1;i<=n;i++) cin>>b[i];
    for (int i=1;i<=n;i++)
    {
        int x=0,y=0;
        for (int j=i;j<=n;j++)
        {
            x=max(x,a[j]);
            y=max(y,b[j]);
            f[i][j]=x*y;
        }
    }
    for (int len=2;len<=n;len++)
        for (int i=1;i+len-1<=n;i++)
        {
            int j=i+len-1;
            f[i][j]+=f[i+1][j]+f[i][j-1]-f[i+1][j-1];
        }
    cin>>q;
    while (q--)
    {
        int l,r;
        cin>>l>>r;
        cout<<f[l][r]<<'\n';
    }
}
signed main()
{
    freopen("match.in","r",stdin);
    freopen("match.out","w",stdout);
    IOS && cin.tie(0);
    int T;
    cin>>T;
    T=1;
    while (T--)
    {
        BellaKira();
    }
}