#include <iostream>
#include <algorithm>

#define ull unsigned long long

inline ull read() {
	ull x = 0;
	char ch = getchar();
	while (ch == ' ' || ch == '\n' || ch == '\t') ch = getchar();
	while (true) {
		if (ch >= '0' && ch <= '9') x = 10 * x + ch - '0';
		else break;
		ch = getchar();
	}
	return x;
}

void write(ull x) {
	if (x < 10) {
		putchar('0' + x);
		return;
	}
	write(x / 10);
	putchar('0' + x % 10);
}

ull T, n, Q;
ull aRange[500005][2], bRange[500005][2];
ull ast[500005][25], bst[500005][25];
ull _log2[250005];

void buildSt(ull st[500005][25]) {
	for (int j = 1; j <= _log2[n] + 1; j++) {
		for (int i = 1; i <= n; i++) {
			st[i][j] = std::max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
		}
	}
}

inline ull query(ull st[500005][25], const int l, const int r) {
	return std::max(st[l][_log2[r - l + 1]], st[r + 1 - (1 << (_log2[r - l + 1]))][_log2[r - l + 1]]);
}

int main() {
	freopen("match.in", "r", stdin);
	freopen("match.out", "w", stdout);
	T = read(), n = read();
	_log2[1] = 0;
	for (int i = 2; i <= n; i++) _log2[i] = _log2[i >> 1] + 1;
	for (int i = 1; i <= n; i++) ast[i][0] = read();
	for (int i = 1; i <= n; i++) bst[i][0] = read();
	buildSt(ast);
	buildSt(bst);
	Q = read();
	for (int i = 0; i < Q; i++) {
		ull l = read(), r = read();
		ull ans = 0;
		for (int p = l; p <= r; p++) {
			for (int q = p; q <= r; q++) {
				ans += query(bst, p, q) * query(ast, p, q);
			}
		}
		write(ans);
		putchar('\n');
	}
	return 0;
}
/*
0 10
1 3 4 6 2 8 1 2 3 5
1 3 4 6 2 8 1 2 3 5

*/