#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
#define ll long long
#define int ll
const int mxn = (int)2e5+7;
const int MOD = 998244353;
#define isON(n, k) (((n) >> (k)) & 1)
void GETAC(){
int n, k, q;
cin >> n >> k;
vector<int> a(n);
for (int& i : a) cin >> i;
cin >> q;
vector<int> pre(n+1); // preprocess subarray sums
for (int i(1); i <= n; ++i)
pre[i] = pre[i-1] + a[i-1];
while (q--)
{
int L, R, ans(0);
cin >> L >> R;
/*
* for (int l(L); l <= R; ++l)
for (int r(l); r <= R; ++r)
if (pre[r] - pre[l-1] == k)
++ans;
*
*/
map<int, int> f; // key : sum, value : frequency sum appears
f[pre[L-1]] = 1;
for (int r(L); r <= R; ++r) { // right ends
if (pre[r] - k >= 0)
ans += f[pre[r] - k]; // left ends satisfy pre[r] - pre[l-1] == k
++f[pre[r]];
}
cout << ans << '\n';
}
}
signed main()
{
fastio
int t(1); cin >> t;
while(t--) GETAC(), cout << '\n';
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZmFzdGlvIGlvczo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKG51bGxwdHIpOyBjb3V0LnRpZShudWxscHRyKTsKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBpbnQgbGwKY29uc3QgaW50IG14biA9IChpbnQpMmU1Kzc7CmNvbnN0IGludCBNT0QgPSA5OTgyNDQzNTM7CiNkZWZpbmUgaXNPTihuLCBrKSAoKChuKSA+PiAoaykpICYgMSkKCnZvaWQgR0VUQUMoKXsKCiAgICBpbnQgbiwgaywgcTsKICAgIGNpbiA+PiBuID4+IGs7CiAgICB2ZWN0b3I8aW50PiBhKG4pOwogICAgZm9yIChpbnQmIGkgOiBhKSBjaW4gPj4gaTsKCiAgICBjaW4gPj4gcTsKICAgIHZlY3RvcjxpbnQ+IHByZShuKzEpOyAvLyBwcmVwcm9jZXNzIHN1YmFycmF5IHN1bXMKICAgIGZvciAoaW50IGkoMSk7IGkgPD0gbjsgKytpKQogICAgICAgIHByZVtpXSA9IHByZVtpLTFdICsgYVtpLTFdOwoKICAgIHdoaWxlIChxLS0pCiAgICB7CgogICAgICAgIGludCBMLCBSLCBhbnMoMCk7CiAgICAgICAgY2luID4+IEwgPj4gUjsKICAgICAgICAvKgogICAgICAgICAqICBmb3IgKGludCBsKEwpOyBsIDw9IFI7ICsrbCkgCiAgICAgICAgICAgICAgICBmb3IgKGludCByKGwpOyByIDw9IFI7ICsrcikgCiAgICAgICAgICAgICAgICAgICAgaWYgKHByZVtyXSAtIHByZVtsLTFdID09IGspIAogICAgICAgICAgICAgICAgICAgICAgICArK2FuczsKICAgICAgICAgKiAKICAgICAgICAgKi8KICAgICAgICBtYXA8aW50LCBpbnQ+IGY7IC8vIGtleSA6IHN1bSwgdmFsdWUgOiBmcmVxdWVuY3kgc3VtIGFwcGVhcnMKICAgICAgICBmW3ByZVtMLTFdXSA9IDE7CiAgICAgICAgZm9yIChpbnQgcihMKTsgciA8PSBSOyArK3IpIHsgLy8gcmlnaHQgZW5kcwogICAgICAgICAgICBpZiAocHJlW3JdIC0gayA+PSAwKQogICAgICAgICAgICAgICAgYW5zICs9IGZbcHJlW3JdIC0ga107IC8vIGxlZnQgZW5kcyBzYXRpc2Z5IHByZVtyXSAtIHByZVtsLTFdID09IGsKICAgICAgICAgICAgKytmW3ByZVtyXV07CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgYW5zIDw8ICdcbic7CiAgICB9Cn0KCnNpZ25lZCBtYWluKCkKewogICAgZmFzdGlvCiAgICBpbnQgdCgxKTsgY2luID4+IHQ7CiAgICB3aGlsZSh0LS0pIEdFVEFDKCksIGNvdXQgPDwgJ1xuJzsKfQoK