def solve(): n, K = map(int, input().split()) A = list(map(int, input().split())) 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) return ma <= tot // K A.sort() rr = A[-K] - 1 + K ans = 1 << 60 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 tot = 0 mac = 0 r = r * K + d 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 minus = min(mac, tot - r) tot -= minus ans = min(ans, tot) print(ans) for _ in range(int(input())): solve()