#include <atcoder/all>
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace atcoder;
using namespace std;

typedef long long ll;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    rep(i, 0, n) cin >> a[i];
    {
        ll sm = 0;
        rep(i, 0, n) { sm += a[i]; }
        if (sm % k != 0) {
            cout << "-1\n";
            return;
        }
    }
    priority_queue<int> pq;
    ll sm = 0;
    rep(i, 0, n) {
        a[i] = min(a[i], k * k + a[i] % k);
        pq.push(a[i]);
        sm += a[i];
    }
    ll ans = 1e9;
    while (1) {
        if ((ll)pq.top() <= sm / k) {
            ans = min(ans, sm / k);
        }
        int tp = pq.top();
        pq.pop();
        if (tp < k)
            break;
        sm -= k;
        pq.push(tp - k);
    }
    if (ans == 1e9)
        ans = -1;
    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}