#include <bits/stdc++.h>
using namespace std;
const int N = 250000 + 10;
int n, a[N], b[N];
int main(void)
{
    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    int Q;
    cin >> Q;
    while(Q--)
    {
        int l, r;
        cin >> l >> r;
        unsigned long long ans = 0;
        for(int p = l; p <= r; p++)
        {
            int mx = a[p], mx2 = b[p];
            for(int q = p; q <= r; q++)
            {
                mx = max(mx, a[q]);
                mx2 = max(mx2, b[q]);
                ans += (unsigned long long)mx * mx2;
            }
        }
        cout << ans << '\n';
    }
}