# correct from heapq import heappush, heappushpop def solve(n: int, k: int, a: list[int]) -> None: if sum(a) % k: print(-1) return pq: list[int] = [] threshold = k * (k - 1) // 2 for i in range(n): q = (max(0, a[i] - threshold) + (k - 1)) // k a[i] -= q * k heappush(pq, -a[i]) INF = 1 << 30 ans = INF t = sum(a) // k while True: e = -pq[0] if e <= t: ans = t e -= k if e < 0: break heappushpop(pq, -e) t -= 1 if ans == INF: print(-1) else: print(ans) t = int(input()) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) solve(n, k, a)