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: int) -> bool: tot = 0 x_k = x % K for a, c in zip(A, C): if a < x: tot += a else: if x < c: return False if c <= x_k: tot += x - x_k + c else: tot += x - x_k + c - K return x <= tot // K rr = A[n - K] - 1 + K inf = 1 << 62 max_c = inf for d in range(K): ma = (rr - d) // K * K + d if not ok(ma): continue l = -1 r = ma // K while r - l > 1: mid = (l + r) >> 1 if ok(mid * K + d): r = mid else: l = mid if r * K + d < max_c: max_c = r * K + d x = max_c tot = 0 max_cnt = 0 x_k = x % K for a, c in zip(A, C): if a < x: tot += a else: if c <= x_k: tot += x - x_k + c if c == x_k: max_cnt += 1 else: tot += x - x_k + c - K tot //= K if max_c < K: max_cnt = 0 ans = max(max_c, tot - max_cnt) if ans == inf: print(-1) else: print(ans) for _ in range(int(input())): solve()