#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";



    }


}


