#include<bits/stdc++.h>
using namespace std;
int n, m;
int a[30000], b[30000];
int main() {
	freopen("match.in", "r", stdin);
//	freopen("match.out", "w", stdout);
int t; cin >> t; int q;
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= m; i++) cin >> b[i];
	cin >> q;
	for (int i = 1; i <= q; i++) {
		int l,r; cin >> l >> r; unsigned long long ans = 0;
		for (int j = l; j <= r; j++) {
			unsigned long long amax = 0, bmax = 0;
			for (int k = j; k <= r; k++) {
				amax = max(amax, (unsigned long long)a[k]); bmax = max(bmax, (unsigned long long)b[k]);
				ans = ans + amax * bmax;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
