#include <bits/stdc++.h>
#define rd freopen("input.txt", "r", stdin)
#define rw freopen("output.txt", "w", stdout)
#define gg ios_base::sync_with_stdio(0)
using namespace std;
long long t, n, m, x, v, ar[100100], as[100100], t1, t2, res;
int main() {
gg;
//rd;
cin >> t;
while (t--) {
res = 0;
cin >> n >> m >> x;
memset(ar, 0, sizeof(ar));
memset(as, 0, sizeof(as));
for (int i = 0; i < n; i++) {
cin >> v;
v = v%m;
ar[v]++;
}
as[0] = 0;
for (int i = 1; i < m; i++) {
as[i] = as[i - 1] + ar[i];
}
as[m] = as[m - 1] + ar[0];
for (int i = 0; i < m; i++) {
if (i<=x) {
res += as[x - i]*ar[i];
res += (as[m] - as[m - i - 1])*ar[i];
}
else {
res += (as[m + x - i] - as[m - i - 1])*ar[i];
}
}
cout << res << endl;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIHJkIGZyZW9wZW4oImlucHV0LnR4dCIsICJyIiwgc3RkaW4pCiNkZWZpbmUgcncgZnJlb3Blbigib3V0cHV0LnR4dCIsICJ3Iiwgc3Rkb3V0KQojZGVmaW5lIGdnIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCkKIAp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKbG9uZyBsb25nIHQsIG4sIG0sIHgsIHYsIGFyWzEwMDEwMF0sIGFzWzEwMDEwMF0sIHQxLCB0MiwgcmVzOwoKaW50IG1haW4oKSB7CglnZzsKCS8vcmQ7CgoJY2luID4+IHQ7Cgl3aGlsZSAodC0tKSB7CgkJcmVzID0gMDsKCQljaW4gPj4gbiA+PiBtID4+IHg7CgkKCQltZW1zZXQoYXIsIDAsIHNpemVvZihhcikpOwoJCW1lbXNldChhcywgMCwgc2l6ZW9mKGFzKSk7CgoJCWZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CgkJCWNpbiA+PiB2OwoJCQl2ID0gdiVtOwoJCQlhclt2XSsrOwoJCX0KCQlhc1swXSA9ID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