Chef's dog Snuffles has so many things to play with! This time around, Snuffles has an array A containing N integers: A1, A2, ..., AN.
Bad news: Snuffles only loves to play with an array in which all the elements are equal.
Good news: We have a mover of size D. ! 
A mover of size D is a tool which helps to change arrays. Chef can pick two existing elements Ai and Aj from the array, such that i + D = j and subtract 1 from one of these elements (the element should have its value at least 1), and add 1 to the other element. In effect, a single operation of the mover, moves a value of 1 from one of the elements to the other.
Chef wants to find the minimum number of times she needs to use the mover of size D to make all the elements of the array A equal. Help her find this out.

-----Input-----
- The first line of the input contains an integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains two integers N and D, denoting the number of elements in the array and the size of the mover. 
- The second line of each testcase contains N space-separated integers: A1, A2, ..., AN, denoting the initial elements of the array. 

-----Output-----
- For each test case, output a single line containing the minimum number of uses or -1 if it is impossible to do what Snuffles wants.

-----Constraints-----
- 1 ≤ T ≤ 10
- 2 ≤ N ≤ 105
- 1 ≤ D < N
- 1 ≤ Ai ≤ 109

-----Subtasks-----
- Subtask 1 (30 points) : N ≤  103
- Subtask 2 (70 points) : Original constraints

-----Example-----
Input:
3
5 2
1 4 5 2 3
3 1
1 4 1
4 2
3 4 3 5

Output:
3
2
-1

-----Explanation-----
Testcase 1:
Here is a possible sequence of usages of the mover:
- Move 1 from A3 to A1
- Move 1 from A3 to A1
- Move 1 from A2 to A4
At the end, the array becomes (3, 3, 3, 3, 3), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 3.
Testcase 2:
Here is a possible sequence of usages of the mover:
- Move 1 from A2 to A1
- Move 1 from A2 to A3
At the end, the array becomes (2, 2, 2), which Snuffles likes. And you cannot achieve this in fewer moves. Hence the answer is 2.
Testcase 3:
It is impossible to make all the elements equal. Hence the answer is -1.

Difficulty: UNKNOWN_DIFFICULTY

Solutions:
T = int(input())
for i in range(0, T):
	(N, D) = input().split()
	N = int(N)
	D = int(D)
	L = [int(i) for i in input().split()]
	A = 0
	flag = ''
	MOVE = 0
	s = 0
	s = sum(L)
	if s % len(L) != 0:
		print(-1)
		flag = False
	else:
		A = s / len(L)
		for i in range(N):
			if L[i] > A and i + D < N:
				diff = L[i] - A
				L[i] = L[i] - diff
				L[i + D] = L[i + D] + diff
				MOVE = MOVE + diff
			elif L[i] < A and i + D < N:
				diff = A - L[i]
				L[i] = L[i] + diff
				L[i + D] = L[i + D] - diff
				MOVE = MOVE + diff
		for i in L:
			if i != A and flag != False:
				print(-1)
				flag = False
				break
		if flag != False:
			print(int(MOVE))


w = int(input())

def checkEqual(lst):
	return lst[1:] == lst[:-1]
v = 0
while v < w:
	sign = 0
	v += 1
	[n, d] = [int(i) for i in input().split()]
	arr = list(map(int, input().split()))
	my_dict = {}
	i = 0
	while i < d:
		my_dict[i] = list()
		j = 0
		while i + j < n:
			my_dict[i].append(arr[i + j])
			j += d
		i += 1
	mean = float(sum(arr)) / len(arr)
	if mean % 1 != 0.0:
		print(-1)
		continue
	else:
		means = []
		for i in range(d):
			means.append(sum(my_dict[i]) / len(my_dict[i]))
		if not checkEqual(means):
			print(-1)
			continue
		steps = 0
		for x in range(d):
			for y in range(len(my_dict[x])):
				if my_dict[x][y] < mean:
					steps += mean - my_dict[x][y]
					my_dict[x][y + 1] -= mean - my_dict[x][y]
					my_dict[x][y] = mean
				elif my_dict[x][y] > mean:
					steps += my_dict[x][y] - mean
					my_dict[x][y + 1] += my_dict[x][y] - mean
					my_dict[x][y] = mean
				elif my_dict[x][y] == mean:
					continue
				else:
					print('impossible')
		print(int(steps))


t = int(input())
while t != 0:
	(n, d) = list(map(int, input().strip().split()))
	a = input().strip()
	a = a.split()
	c = 0
	b = []
	c = sum((int(i) for i in a))
	if c % n != 0:
		print('-1')
	else:
		m = 0
		b += n * [c / n]
		f = 1
		for i in range(0, len(a) - d):
			if a.count(a[0]) == len(a):
				f = 0
				break
			elif int(a[i]) > int(b[i]):
				temp = int(a[i]) - int(b[i])
				a[i] = int(b[i])
				a[i + d] = int(a[i + d]) + temp
				m = m + temp
			elif int(a[i]) < int(b[i]):
				temp = int(b[i]) - int(a[i])
				a[i] = int(b[i])
				a[i + d] = int(a[i + d]) - temp
				m = m + temp
			if i == len(a) - d - 1 and all((int(x) == int(a[0]) for x in a)):
				f = 0
		if f == 0:
			print(m)
		else:
			print('-1')
	t = t - 1


import sys
t = int(sys.stdin.readline().strip())
while t:
	(n, d) = list(map(int, sys.stdin.readline().strip().split(' ')))
	nums = list(map(int, sys.stdin.readline().strip().split(' ')))
	a = False
	s = 0
	for i in nums:
		s = s + i
	avg = s // n
	ans = 0
	count = 0
	if s % n != 0:
		ans = -1
	else:
		arr = [avg] * n
		for i in range(len(arr) - d):
			k = arr[i] - nums[i]
			if k != 0:
				arr[i] = arr[i] - k
				arr[i + d] = arr[i + d] + k
				count += abs(k)
		for i in range(len(arr)):
			if arr[i] != nums[i]:
				ans = -1
				a = True
				break
		if a != True:
			ans = count
	print(ans)
	t -= 1