def solve(): n, K = map(int, input().split()) A = list(map(int, input().split())) A.sort() C = [a % K for a in A] if sum(C) % K != 0: print(-1) return def ok(x): tot = 0 ma = 0 for a, c in zip(A, C): d = (x - c) // K * K + c d = min(d, a) tot += d ma = max(ma, d) if d < 0: return False return x <= tot // K rr = A[-K] - 1 + K inf = 1 << 62 ans = inf for d in range(K): ma = (rr - d) // K * K + d if not ok(ma): continue r = ma // K l = -1 while r - l > 1: mid = (l + r) // 2 if ok(mid * K + d): r = mid else: l = mid r = r * K + d tot = 0 mac = 0 for a, c in zip(A, C): d = (r - c) // K * K + c d = min(d, a) tot += d if d == r: mac += 1 tot //= K if r <= K: mac = 0 tmp = max(r, tot - mac) ans = min(ans, tmp) if ans == inf: print(-1) else: print(ans) for _ in range(int(input())): solve()