#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; using mint = modint998244353; // void solve() { // int n; // cin >> n; // vector<int> a(n); // rep(i, 0, n) { // cin >> a[i]; // if (a[i] > 0) // a[i]--; // } // mint ans = 0; // cout << ans.val() << endl; // } template <typename T> T ceil_div(T a, T b) { if (b < 0) a = -a, b = -b; return (a >= 0 ? (a + b - 1) / b : a / b); } 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; } } vector<int> b(n); rep(i, 0, n) { b[i] = a[i] % k; } sort(b.begin(), b.end()); if (b.back() == 0) { cout << "0\n"; return; } int las = b.back(); int smbp = 0; rep(i, 0, n - 1) { smbp += b[i]; } if (ceil_div(smbp, k - 1) >= las) { cout << (smbp + las) / k << '\n'; return; } auto f = [&]() { int m = las + k; auto c = a; ll sm = 0; rep(i, 0, n) { sm += min(c[i], (ll)m); } return sm >= (ll)m * (ll)k; }; bool ok = f(); if (!ok) { cout << "-1\n"; return; } cout << las + k << '\n'; } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int t; cin >> t; while (t--) { solve(); } }