#include<bits/stdc++.h>
#define int long long //__int128
const int MAXN = 1e5+3;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;//0x3f3f3f3f3f3f3f3f;
using namespace std;
int n,a[MAXN],b[MAXN],q;
inline unsigned long long query(int L,int R) {
	unsigned long long  ans = 0;
	for(int i = 1; i<=R-L+1; i++) {
		for(int l = L,r = L+i-1; r<=R; l++,r++) {
			int maxxa = 0,maxxb = 0;
			for(int j = l; j<=r; j++) {
				maxxa = max(maxxa,a[j]),maxxb = max(maxxb,b[j]);
			}
			ans+=maxxa*maxxb;
//			cout<<l<<" "<<r<<" "<<maxxa*maxxsb<<"\n";
		}
	}
	return ans;
}
int T;
void work() {
	cin>>T;
	cin>>n;
	for(int i = 1; i<=n; i++)cin>>a[i];
	for(int i = 1; i<=n; i++)cin>>b[i];
	cin>>q;
	for(int l,r,i = 1; i<=q; i++) {
		cin>>l>>r;
		cout<<query(l,r)<<"\n";
	}
	return;
}
signed main() {
	// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	freopen("match.in","r",stdin);
	freopen("match.out","w",stdout);

	// while(T--)
	work();
	return 0;
}
