#define _GLIBCXX_FILESYSTEM
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define f first
#define s second
using namespace std;
int n;
int main()
{
IOS
int t;
cin>>t;
while(t--)
{
int k;
cin>>n>>k;
vector<int> vec(n+1);
for(int i=1; i<=n; i++)
cin>>vec[i];
if(n==1)
{
cout<<0<<"\n";
continue;
}
sort(vec.begin()+1,vec.end());
ll out=LLONG_MAX;
for(int j=1; j<=n; j++)
{
map<int,int> mp;
int mtch=0;
ll ans=0;
for(int i=1; i<=n; i++)
{
if(i==j&&n&1)continue;
ll md=vec[i]%k;
if(mp[md]==0)
{
mp[md]=i;
mtch++;
}
else
{
ll clc=abs(vec[mp[md]]-vec[i])/k;
ans+=clc;
mp[md]=0;
mtch--;
}
}
if(mtch==0)
out=min(out,ans);
if(n&1==0) break;
}
if(out==LLONG_MAX)cout<<"-1\n";
else cout<<out<<"\n";
}
}
I2RlZmluZSBfR0xJQkNYWF9GSUxFU1lTVEVNCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgojZGVmaW5lIElPUyBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsgY291dC50aWUoMCk7CiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHMgc2Vjb25kCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCmludCBuOwoKaW50IG1haW4oKQp7CgoKICAgIElPUwogICAgaW50IHQ7CiAgICBjaW4+PnQ7CiAgICB3aGlsZSh0LS0pCiAgICB7CiAgICAgICAgaW50IGs7CiAgICAgICAgY2luPj5uPj5rOwogICAgICAgIHZlY3RvcjxpbnQ+IHZlYyhuKzEpOwogICAgICAgIGZvcihpbnQgaT0xOyBpPD1uOyBpKyspCiAgICAgICAgICAgIGNpbj4+dmVjW2ldOwogICAgICAgIGlmKG49PTEpCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDwwPDwiXG4iOwogICAgICAgICAgICBjb250aW51ZTsKICAgICAgICB9CgogICAgICAgIHNvcnQodmVjLmJlZ2luKCkrMSx2ZWMuZW5kKCkpOwoKICAgICAgICBsbCBvdXQ9TExPTkdfTUFYOwogICAgICAgIGZvcihpbnQgaj0xOyBqPD1uOyBqKyspCiAgICAgICAgewogICAgICAgICAgICBtYXA8aW50LGludD4gbXA7CiAgICAgICAgICAgIGludCBtdGNoPTA7CiAgICAgICAgICAgIGxsIGFucz0wOwogICAgICAgICAgICBmb3IoaW50IGk9MTsgaTw9bjsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZihpPT1qJiZuJjEpY29udGludWU7CiAgICAgICAgICAgICAgICBsbCBtZD12ZWNbaV0lazsKICAgICAgICAgICAgICAgIGlmKG1wW21kXT09MCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBtcFttZF09aTsKICAgICAgICAgICAgICAgICAgICBtdGNoKys7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbGwgY2xjPWFicyh2ZWNbbXBbbWRdXS12ZWNbaV0pL2s7CiAgICAgICAgICAgICAgICAgICAgYW5zKz1jbGM7CiAgICAgICAgICAgICAgICAgICAgbXBbbWRdPTA7CiAgICAgICAgICAgICAgICAgICAgbXRjaC0tOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKG10Y2g9PTApCiAgICAgICAgICAgICAgICBvdXQ9bWluKG91dCxhbnMpOwoKICAgICAgICAgICAgaWYobiYxPT0wKSBicmVhazsKCiAgICAgICAgfQoKICAgICAgICBpZihvdXQ9PUxMT05HX01BWCljb3V0PDwiLTFcbiI7CiAgICAgICAgZWxzZSBjb3V0PDxvdXQ8PCJcbiI7CgoKCiAgICB9CgoKfQoKCg==
MTEKMSAxMDAwMDAwMDAwCjEKMiAxCjYyNDMyMzc5OSA3MDgyOTAzMjMKMyAxCjMgMiAxCjQgMQo3IDEgNSAzCjUgMQoxMSAyIDE1IDcgMTAKNyAxCjEgOCAyIDE2IDggMTYgMzEKMTMgMQoyIDEgMSAzIDMgMTEgMTIgMjIgNDUgNzc3IDc3NyAxNTAwIDc0CjEwIDIKMSAyIDEgMiAxIDIgMSAyIDEgMgoxMSAyCjEgMiAxIDIgMSAyIDEgMiAxIDIgMQoxMyAzCjIgMyA5IDE0IDE3IDEwIDIyIDIwIDE4IDMwIDEgNCAyOAo1IDEKMiAzIDUgMyA1Cg==
11
1 1000000000
1
2 1
624323799 708290323
3 1
3 2 1
4 1
7 1 5 3
5 1
11 2 15 7 10
7 1
1 8 2 16 8 16 31
13 1
2 1 1 3 3 11 12 22 45 777 777 1500 74
10 2
1 2 1 2 1 2 1 2 1 2
11 2
1 2 1 2 1 2 1 2 1 2 1
13 3
2 3 9 14 17 10 22 20 18 30 1 4 28
5 1
2 3 5 3 5