#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

namespace SuperI{
	const int MBUF = 2 << 21;
	std::streambuf *inbuf = std::cin.rdbuf();
	char buf[MBUF], *head = buf, *tail = buf, ___c;
	inline char getchar() {
		if ((head == tail) && (tail = (head = buf) + inbuf->sgetn(buf, MBUF), head == tail)) return EOF;
		else return *head++;
	}
	inline void read(int *const p) {
		*p = 0;
		for (___c = getchar(); ___c < '0' || ___c > '9';)
		___c = getchar();
		for (; ___c >= '0' && ___c <= '9'; *p = *p * 10 + (___c ^ 48), ___c = getchar());
	}
	inline void read(long long *const p) {
		*p = 0;
		for (___c = getchar(); ___c < '0' || ___c > '9'; ___c = getchar());
		for (; ___c >= '0' && ___c <= '9'; *p = *p * 10 + (___c ^ 48), ___c = getchar());
	}
}

const int MAXN = 3e5 + 5;
int T, n, q;
int a[MAXN], b[MAXN];
unsigned long long prea[MAXN], preb[MAXN];

namespace A {
int sta[MAXN][20], stb[MAXN][20], logt[MAXN];
int main() {
	using SuperI::read;
	for (int i = 1; i <= n; ++i) sta[i][0] = a[i];
	for (int i = 1; i <= n; ++i) stb[i][0] = b[i];
	for (int k = 1; k <= 20; ++k)
		for (int i = 1; i + (1 << k) - 1 <= n; ++i)
			sta[i][k] = std::max(sta[i][k - 1], sta[i + (1 << (k - 1))][k - 1]),
			stb[i][k] = std::max(stb[i][k - 1], stb[i + (1 << (k - 1))][k - 1]);
	for (int i = 2; i <= n; ++i) logt[i] = logt[i / 2] + 1;
	for (int k = 1; k <= q; ++k) {
		unsigned long long ans = 0ll;
		int l, r;
		read(&l);
		read(&r);
		for (int i = l; i <= r; ++i) for (int j = i; j <= r; ++j)
			ans += (unsigned long long)(std::max(sta[i][logt[j - i + 1]], sta[j - (1 << logt[j - i + 1]) + 1][logt[j - i + 1]])) * (unsigned long long)(std::max(stb[i][logt[j - i + 1]], stb[j - (1 << logt[j - i + 1]) + 1][logt[j - i + 1]]));
		std::cout << ans << '\n';
	}
	return 0;
}
}
namespace B {
int sta[MAXN][20], stb[MAXN][20], bl, l_b[MAXN];
struct Query {
	int l, r, pos;
	Query(int ___l = 0, int ___r = 0, int ___pos = 0)
		: l(___l), r(___r), pos(___pos) {}
}qry[MAXN];
int main() {
	for (int i = 1; i <= n; ++i) sta[i][0] = a[i];
	for (int i = 1; i <= n; ++i) stb[i][0] = b[i];
	for (int k = 1; k <= 20; ++k)
		for (int i = 1; i + (1 << k) - 1 <= n; ++i)
			sta[i][k] = std::max(sta[i][k - 1], sta[i + (1 << (k - 1))][k - 1]),
			stb[i][k] = std::max(stb[i][k - 1], stb[i + (1 << (k - 1))][k - 1]);
	bl = std::sqrt(n);
	for (int i = 1; i <= n; ++i) 
	return 0;
}
}

int main() {
	using SuperI::read;
	freopen("match.in", "r", stdin);
	freopen("match.out", "w", stdout);
	
	read(&T);
	read(&n);
	for (int i = 1; i <= n; ++i) read(a + i);
	for (int i = 1; i <= n; ++i) read(b + i);
	read(&q);
//	if (q <= 3000) 
	A::main();
//	B::main();

	fclose(stdin);
	fclose(stdout);
	return 0;
}
