#include using namespace std; typedef long long ll; const double pi = acos(-1); const int maxn = 100000; int n, q, a[maxn + 10], b[maxn + 10], bel[maxn + 10], t; int l, lgl, blksz; ll f[401][maxn + 10]; struct cplx { double x, y; }wn[maxn * 4 + 10], c[maxn * 4 + 10], d[maxn * 4 + 10]; int rev[maxn * 4 + 10]; cplx operator + (const cplx &a, const cplx &b) { return (cplx){a.x + b.x, a.y + b.y}; } cplx operator - (const cplx &a, const cplx &b) { return (cplx){a.x - b.x, a.y - b.y}; } cplx operator * (const cplx &a, const cplx &b) { return (cplx){a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x}; } void prework(int n) { l = 1; lgl = -1; while (l <= n) { l <<= 1; ++lgl; } for (int i = 0; i < l; ++i) { rev[i] = rev[i >> 1] >> 1 | (i & 1) << lgl; wn[i] = (cplx){cos(i * pi / l), sin(i * pi / l)}; } } void dft(cplx *a) { for (int i = 0; i < l; ++i) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int i = 1; i < l; i <<= 1) for (int j = 0; j < l; j += i << 1) for (int k = 0; k < i; ++k) { cplx x = a[j + k], y = wn[k * (l / i)] * a[i + j + k]; a[j + k] = x + y; a[i + j + k] = x - y; } } void idft(cplx *a) { reverse(a + 1, a + l); dft(a); for (int i = 0; i < l; ++i) a[i].x /= l; } ll calc(int x, int y, int len) { ll ans = 0; for (int i = 0; i < len; ++i) ans += a[x + i] * b[y + i]; return ans; } int main() { scanf("%d", &t); while (t--) { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) scanf("%d", &b[i]); blksz = sqrt(n) * 15; prework(n + blksz); for (int i = 0; i < l; ++i) c[i].x = c[i].y = d[i].x = d[i].y = 0; for (int i = 1; i <= n; ++i) { c[i].x = a[i]; bel[i] = (i - 1) / blksz + 1; } dft(c); for (int i = 1; i * blksz <= n; ++i) { for (int j = 0; j < l; ++j) d[j].x = d[j].y = 0; for (int j = 1; j <= blksz; ++j) d[blksz + 1 - j].x = b[(i - 1) * blksz + j]; dft(d); for (int j = 0; j < l; ++j) d[j] = d[j] * c[j]; idft(d); for (int j = 1; j <= n; ++j) f[i][j] = d[j + blksz].x + 0.5; } while (q--) { int x, y, len; scanf("%d%d%d", &x, &y, &len); if (bel[y] == bel[y + len - 1]) printf("%lld\n", calc(x, y, len)); else { ll ans = calc(x, y, bel[y] * blksz - y + 1) + calc(x + blksz * (bel[y + len - 1] - 1) + 1 - y, blksz * (bel[y + len - 1] - 1) + 1, y + len - 1 - blksz * (bel[y + len - 1] - 1)); for (int j = bel[y] + 1; j <= bel[y + len - 1] - 1; ++j) ans += f[j][x + (j - 1) * blksz + 1 - y]; printf("%lld\n", ans); } } } }