#include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define int long long

using namespace std;

int T, n, a[300005], b[300005];

int calc(int l, int r) {
	int res = 0;
	rep(i, l, r) {
		rep(j, l, r) {
			res = max(res, a[i] * b[j]);
		}
	}
	return res;
}

void solve() {
	cin >> n;
	rep(i, 1, n) cin >> a[i];
	rep(i, 1, n) cin >> b[i];
	int q;
	cin >> q;
	while (q--) {
		int l, r; cin >> l >> r;
		unsigned long long ans = 0;
		rep(i, l, r) {
			rep(j, i, r) {
				ans += calc(i, j);
			}
		}
		cout << ans << '\n';
	}
}

signed main() {
	freopen("match.in", "r", stdin);
	freopen("match.out", "w", stdout);
	cin.tie(nullptr)->sync_with_stdio(false);
	cin >> T;
	solve();
	return 0;
}
