#include <bits/stdc++.h>

using namespace std;

namespace IO{
    template <typename T> void read(T &x){
        bool f = false;
        char c;
        x = 0;
        while (!isdigit(c = getchar())) if (c == '-') f = true;
        while (isdigit(c)) x = 10 * x + c - '0', c = getchar();
        if (f) x = -x;
    }
    template <typename T, typename... Args> void read(T &x, Args&... args){read(x); read(args...);}
} using namespace IO;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;

const int M = 2.5e5 + 10;

int T, Q, n;
int a[M], b[M], ma[M], mb[M];
map<P, ULL> h;
stack<int> q;

int main(){

    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);

    read(T, n);
    for (int i = 1; i <= n; i++) read(a[i]);
    for (int i = 1; i <= n; i++) read(b[i]);

    int l, r;
    ULL ans;
    read(Q);
    while (Q--){
        ans = 0;
        
        read(l, r);
        if (h.count({l, r})){
            printf("%llu\n", h[{l, r}]);
            continue;
        }

        for (int i = l; i <= r; i++){
            ma[i - 1] = 0;
            mb[i - 1] = 0;
            for (int j = i; j <= r; j++){
                if (a[j] > ma[j - 1]) ma[j] = a[j];
                else ma[j] = ma[j - 1];
                if (b[j] > mb[j - 1]) mb[j] = b[j];
                else mb[j] = mb[j - 1];
                ans += (ULL)ma[j] * (ULL)mb[j];

            }
        } 
        printf("%llu\n", ans);
        continue;
    
    }

    return 0;
}
