#include <bits/stdc++.h>
using namespace std;
void fileIO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
#ifdef ONLINE_JUDGE
#endif
}
#define int long long
struct node {
vector<pair<int, int>> v;
};
struct merge_sort_tree {
int n;
vector<node> tre;
merge_sort_tree(vector<int>& v) {
int LEN = v.size();
n = 1;
while (n < LEN) {
n <<= 1;
}
tre = vector<node>(n << 2, node()), build(v);
}
node merge(node a, node b) {
node t;
int x = 0, y = 0;
while (x < a.v.size() and y < b.v.size()) {
if (a.v[x].first <= b.v[y].first) {
t.v.push_back({ a.v[x].first, a.v[x].first });
++x;
} else {
t.v.push_back({ b.v[y].first, b.v[y].first });
++y;
}
}
while (x < a.v.size()) {
t.v.push_back({ a.v[x].first, a.v[x].first });
++x;
}
while (y < b.v.size()) {
t.v.push_back({ b.v[y].first, b.v[y].first });
++y;
}
for (int i = 1; i < t.v.size(); ++i) {
t.v[i].second += t.v[i - 1].second;
}
return t;
}
void build(vector<long long>& v, int current_node, int lx, int rx) {
if (rx - lx == 1) {
if (lx < v.size()) {
tre[current_node].v.push_back({ v[lx], v[lx] });
}
return;
}
int m = (lx + rx) / 2;
build(v, 2 * current_node + 1, lx, m);
build(v, 2 * current_node + 2, m, rx);
tre[current_node] = merge(tre[2 * current_node + 1], tre[2 * current_node + 2]);
}
void build(vector<long long>& v) {
build(v, 0, 0, n);
}
pair<int, int> less_than_or_equal(int l, int r, int val, int current_node, int lx, int rx) {
if (lx >= r or rx <= l) {
return { 0, 0 };
}
if (rx <= r and lx >= l) {
pair<int, int> p = { val, LLONG_MAX };
auto it = upper_bound(tre[current_node].v.begin(), tre[current_node].v.end(), p);
if (it == tre[current_node].v.begin()) {
return { 0, 0 };
} else {
return { it - tre[current_node].v.begin(), prev(it)->second };
}
}
int m = lx + (rx - lx) / 2;
int left = current_node * 2 + 1;
int right = left + 1;
auto a = less_than_or_equal(l, r, val, left, lx, m);
auto b = less_than_or_equal(l, r, val, right, m, rx);
pair<int, int> ret = { a.first + b.first, a.second + b.second };
return ret;
}
pair<int, int> less_than_or_equal(int l, int r, int val) {
return less_than_or_equal(l, r, val, 0, 0, n);
}
};
void get_shit_done() {
int n, k;
cin >> n >> k;
vector<int> v(n);
map<int, int> fst, lst;
lst[0] = -1, fst[0] = -1;
for (int i = 0, pref = 0; i < n; ++i) {
cin >> v[i];
pref += v[i];
if (not fst.count(pref)) {
fst[pref] = i;
lst[pref] = i;
} else {
lst[pref] = i;
}
}
vector<int> gol(n), gor(n);
for (int i = 0, pref = 0; i < n; ++i) {
if (fst.count(k + pref)) {
gol[i] = max(i, fst[k + pref]);
gor[i] = lst[k + pref];
} else {
gol[i] = n;
gor[i] = n;
}
pref += v[i];
}
merge_sort_tree L(gol), R(gor);
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
auto m = L.less_than_or_equal(l, r + 1, r);
auto p = R.less_than_or_equal(l, r + 1, r);
int calc = p.second + (m.first - p.first) * r - m.second + m.first;
cout << calc << '\n';
}
}
signed main() {
fileIO();
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
int ts = 1;
cin >> ts;
for (int tc = 1; tc <= ts; ++tc) {
get_shit_done();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBmaWxlSU8oKSB7CiNpZm5kZWYgT05MSU5FX0pVREdFCiAgICBmcmVvcGVuKCJpbnB1dC50eHQiLCAiciIsIHN0ZGluKTsKICAgIGZyZW9wZW4oIm91dHB1dC50eHQiLCAidyIsIHN0ZG91dCk7CiNlbmRpZgoKI2lmZGVmIE9OTElORV9KVURHRQoKI2VuZGlmCn0KCiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKc3RydWN0IG5vZGUgewogICAgdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiB2Owp9OwoKc3RydWN0IG1lcmdlX3NvcnRfdHJlZSB7CiAgICBpbnQgbjsKICAgIHZlY3Rvcjxub2RlPiB0cmU7CgogICAgbWVyZ2Vfc29ydF90cmVlKHZlY3RvcjxpbnQ+JiB2KSB7CiAgICAgICAgaW50IExFTiA9IHYuc2l6ZSgpOwogICAgICAgIG4gPSAxOwogICAgICAgIHdoaWxlIChuIDwgTEVOKSB7CiAgICAgICAgICAgIG4gPDw9IDE7CiAgICAgICAgfQogICAgICAgIHRyZSA9IHZlY3Rvcjxub2RlPihuIDw8IDIsIG5vZGUoKSksIGJ1aWxkKHYpOwogICAgfQoKICAgIG5vZGUgbWVyZ2Uobm9kZSBhLCBub2RlIGIpIHsKICAgICAgICBub2RlIHQ7CgogICAgICAgIGludCB4ID0gMCwgeSA9IDA7CiAgICAgICAgd2hpbGUgKHggPCBhLnYuc2l6ZSgpIGFuZCB5IDwgYi52LnNpemUoKSkgewogICAgICAgICAgICBpZiAoYS52W3hdLmZpcnN0IDw9IGIudlt5XS5maXJzdCkgewogICAgICAgICAgICAgICAgdC52LnB1c2hfYmFjayh7IGEudlt4XS5maXJzdCwgYS52W3hdLmZpcnN0IH0pOwogICAgICAgICAgICAgICAgKyt4OwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgdC52LnB1c2hfYmFjayh7IGIudlt5XS5maXJzdCwgYi52W3ldLmZpcnN0IH0pOwogICAgICAgICAgICAgICAgKyt5OwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoeCA8IGEudi5zaXplKCkpIHsKICAgICAgICAgICAgdC52LnB1c2hfYmFjayh7IGEudlt4XS5maXJzdCwgYS52W3hdLmZpcnN0IH0pOwogICAgICAgICAgICArK3g7CiAgICAgICAgfQoKICAgICAgICB3aGlsZSAoeSA8IGIudi5zaXplKCkpIHsKICAgICAgICAgICAgdC52LnB1c2hfYmFjayh7IGIudlt5XS5maXJzdCwgYi52W3ldLmZpcnN0IH0pOwogICAgICAgICAgICArK3k7CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IHQudi5zaXplKCk7ICsraSkgewogICAgICAgICAgICB0LnZbaV0uc2Vjb25kICs9IHQudltpIC0gMV0uc2Vjb25kOwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIHQ7CiAgICB9CgogICAgdm9pZCBidWlsZCh2ZWN0b3I8bG9uZyBsb25nPiYgdiwgaW50IGN1cnJlbnRfbm9kZSwgaW50IGx4LCBpbnQgcngpIHsKICAgICAgICBpZiAocnggLSBseCA9PSAxKSB7CiAgICAgICAgICAgIGlmIChseCA8IHYuc2l6ZSgpKSB7CiAgICAgICAgICAgICAgICB0cmVbY3VycmVudF9ub2RlXS52LnB1c2hfYmFjayh7IHZbbHhdLCB2W2x4XSB9KTsKICAgICAgICAgICAgfQogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQoKICAgICAgICBpbnQgbSA9IChseCArIHJ4KSAvIDI7CiAgICAgICAgYnVpbGQodiwgMiAqIGN1cnJlbnRfbm9kZSArIDEsIGx4LCBtKTsKICAgICAgICBidWlsZCh2LCAyICogY3VycmVudF9ub2RlICsgMiwgbSwgcngpOwoKICAgICAgICB0cmVbY3VycmVudF9ub2RlXSA9IG1lcmdlKHRyZVsyICogY3VycmVudF9ub2RlICsgMV0sIHRyZVsyICogY3VycmVudF9ub2RlICsgMl0pOwogICAgfQoKICAgIHZvaWQgYnVpbGQodmVjdG9yPGxvbmcgbG9uZz4mIHYpIHsKICAgICAgICBidWlsZCh2LCAwLCAwLCBuKTsKICAgIH0KCiAgICBwYWlyPGludCwgaW50PiBsZXNzX3RoYW5fb3JfZXF1YWwoaW50IGwsIGludCByLCBpbnQgdmFsLCBpbnQgY3VycmVudF9ub2RlLCBpbnQgbHgsIGludCByeCkgewogICAgICAgIGlmIChseCA+PSByIG9yIHJ4IDw9IGwpIHsKICAgICAgICAgICAgcmV0dXJuIHsgMCwgMCB9OwogICAgICAgIH0KICAgICAgICBpZiAocnggPD0gciBhbmQgbHggPj0gbCkgewogICAgICAgICAgICBwYWlyPGludCwgaW50PiBwID0geyB2YWwsIExMT05HX01BWCB9OwogICAgICAgICAgICBhdXRvIGl0ID0gdXBwZXJfYm91bmQodHJlW2N1cnJlbnRfbm9kZV0udi5iZWdpbigpLCB0cmVbY3VycmVudF9ub2RlXS52LmVuZCgpLCBwKTsKICAgICAgICAgICAgaWYgKGl0ID09IHRyZVtjdXJyZW50X25vZGVdLnYuYmVnaW4oKSkgewogICAgICAgICAgICAgICAgcmV0dXJuIHsgMCwgMCB9OwogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgcmV0dXJuIHsgaXQgLSB0cmVbY3VycmVudF9ub2RlXS52LmJlZ2luKCksIHByZXYoaXQpLT5zZWNvbmQgfTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaW50IG0gPSBseCArIChyeCAtIGx4KSAvIDI7CiAgICAgICAgaW50IGxlZnQgPSBjdXJyZW50X25vZGUgKiAyICsgMTsKICAgICAgICBpbnQgcmlnaHQgPSBsZWZ0ICsgMTsKCiAgICAgICAgYXV0byBhID0gbGVzc190aGFuX29yX2VxdWFsKGwsIHIsIHZhbCwgbGVmdCwgbHgsIG0pOwogICAgICAgIGF1dG8gYiA9IGxlc3NfdGhhbl9vcl9lcXVhbChsLCByLCB2YWwsIHJpZ2h0LCBtLCByeCk7CiAgICAgICAgcGFpcjxpbnQsIGludD4gcmV0ID0geyBhLmZpcnN0ICsgYi5maXJzdCwgYS5zZWNvbmQgKyBiLnNlY29uZCB9OwoKICAgICAgICByZXR1cm4gcmV0OwogICAgfQoKICAgIHBhaXI8aW50LCBpbnQ+IGxlc3NfdGhhbl9vcl9lcXVhbChpbnQgbCwgaW50IHIsIGludCB2YWwpIHsKICAgICAgICByZXR1cm4gbGVzc190aGFuX29yX2VxdWFsKGwsIHIsIHZhbCwgMCwgMCwgbik7CiAgICB9Cn07Cgp2b2lkIGdldF9zaGl0X2RvbmUoKSB7CiAgICBpbnQgbiwgazsKICAgIGNpbiA+PiBuID4+IGs7CgogICAgdmVjdG9yPGludD4gdihuKTsKICAgIG1hcDxpbnQsIGludD4gZnN0LCBsc3Q7CiAgICBsc3RbMF0gPSAtMSwgZnN0WzBdID0gLTE7CiAgICBmb3IgKGludCBpID0gMCwgcHJlZiA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBjaW4gPj4gdltpXTsKICAgICAgICBwcmVmICs9IHZbaV07CiAgICAgICAgaWYgKG5vdCBmc3QuY291bnQocHJlZikpIHsKICAgICAgICAgICAgZnN0W3ByZWZdID0gaTsKICAgICAgICAgICAgbHN0W3ByZWZdID0gaTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBsc3RbcHJlZl0gPSBpOwogICAgICAgIH0KICAgIH0KCiAgICB2ZWN0b3I8aW50PiBnb2wobiksIGdvcihuKTsKICAgIGZvciAoaW50IGkgPSAwLCBwcmVmID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGlmIChmc3QuY291bnQoayArIHByZWYpKSB7CiAgICAgICAgICAgIGdvbFtpXSA9IG1heChpLCBmc3RbayArIHByZWZdKTsKICAgICAgICAgICAgZ29yW2ldID0gbHN0W2sgKyBwcmVmXTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBnb2xbaV0gPSBuOwogICAgICAgICAgICBnb3JbaV0gPSBuOwogICAgICAgIH0KICAgICAgICBwcmVmICs9IHZbaV07CiAgICB9CiAgICBtZXJnZV9zb3J0X3RyZWUgTChnb2wpLCBSKGdvcik7CgogICAgaW50IHE7CiAgICBjaW4gPj4gcTsKCiAgICB3aGlsZSAocS0tKSB7CiAgICAgICAgaW50IGwsIHI7CiAgICAgICAgY2luID4+IGwgPj4gcjsKCiAgICAgICAgLS1sLCAtLXI7CiAgICAgICAgYXV0byBtID0gTC5sZXNzX3RoYW5fb3JfZXF1YWwobCwgciArIDEsIHIpOwogICAgICAgIGF1dG8gcCA9IFIubGVzc190aGFuX29yX2VxdWFsKGwsIHIgKyAxLCByKTsKCiAgICAgICAgaW50IGNhbGMgPSBwLnNlY29uZCArIChtLmZpcnN0IC0gcC5maXJzdCkgKiByIC0gbS5zZWNvbmQgKyBtLmZpcnN0OwogICAgICAgIGNvdXQgPDwgY2FsYyA8PCAnXG4nOwogICAgfQp9CgpzaWduZWQgbWFpbigpIHsKICAgIGZpbGVJTygpOwogICAgY2luLnRpZShudWxscHRyKTsKICAgIGNvdXQudGllKG51bGxwdHIpOwogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwoKICAgIGludCB0cyA9IDE7CiAgICBjaW4gPj4gdHM7CiAgICBmb3IgKGludCB0YyA9IDE7IHRjIDw9IHRzOyArK3RjKSB7CiAgICAgICAgZ2V0X3NoaXRfZG9uZSgpOwogICAgfQoKICAgIHJldHVybiAwOwp9