#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define sp ' '
#define assert(x,a) if(x){cout << a << endl;return;}
#define sortv(x)sort(x.begin(),x.end())
#define revev(x)reverse(x.begin(),x.end())
inline void fast(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
}
vector<vector<int>>graph;
vector<bool>vis(100001,false);
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
inline void answer(int c){assert(c==0,"NO")cout << "YES" << endl;}
void solve()
{
ll ans1 = 0;
ll n, k; cin >> n >> k;
vector<ll>l(n); for (ll& e : l)cin >> e;
vector<ll>prefix_sum(n + 2);
vector<ll>suffix_sum(n + 2);
multimap<ll,ll>queries;
prefix_sum[1] = l[0];
for (ll i = 2; i <= l.size(); i++)
{
prefix_sum[i] = prefix_sum[i - 1] + l[i - 1];
}
reverse(l.begin(), l.end());
suffix_sum[0] = 0;
suffix_sum[1] = l[0];
queries.insert(pair<ll,ll>(suffix_sum[1] % k,l.size()));
for (ll i = 2; i <= l.size(); i++)
{
suffix_sum[i] = suffix_sum[i - 1] + l[i - 1];
queries.insert(pair<ll,ll>(suffix_sum[i] % k,l.size() - (i - 1)));
}
reverse(suffix_sum.begin(), suffix_sum.end());
for(int i = 1;i < prefix_sum.size()-1;i++)
{
ll ansq = prefix_sum[i] % k;
if(ansq == 0)
{
auto q = queries.upper_bound(0);
auto q1 = queries.lower_bound(0);
for(; q1 != q;q1++)
{
if((*q1).second > i && (prefix_sum[i] + suffix_sum[(*q1).second])% k == 0)
ans1++;
}
}
else
{
auto q = queries.upper_bound(abs(k - (ansq)));
auto q1 = queries.lower_bound(abs(k - (ansq)));
for(; q1 != q;q1++)
{
if((*q1).second > i && (prefix_sum[i] + suffix_sum[(*q1).second] )% k == 0)
ans1++;
}
}
}
cout <<ans1 << endl;
}
int main()
{
fast();
ll t{ 1 };cin >> t;
// cin.ignore();
while (t--)
{
solve();
}
}
Copy