Chef Avi likes the number K. Given a list of N integers, help him find the sum of all distinct numbers repeating exactly K times. Each of those numbers should be considered only once in the sum, not K times.

------ Input Format ------ 

- First line will contain T, number of testcases. Then the testcases follow. 
- Each testcase contains two lines:
- The first line contains two space-separated integers N, K. 
- The second line contains N space-separated integers.

------ Output Format ------ 

For each testcase, print the sum of all numbers occurring exactly K times. If there is no such number, print -1 instead.

------ Constraints ------ 

$1≤ T≤ 10$
$1≤ N≤10,000$
$1 ≤ K ≤ N$
$0 ≤ A_{i} ≤ 10^{9}$

----- Sample Input 1 ------ 
3
4 1
2 3 4 4
5 3
9 9 1 1 9
2 1
2 2
----- Sample Output 1 ------ 
5
9
-1
----- explanation 1 ------ 
Test Case 1: The numbers 2 and 3 occur exactly once. So the answer is 2 + 3 = 5.

Test Case 2: The number 9 occurs exactly three times. So the answer is 9.

Test Case 3: There is no number which occurs exactly once. Hence the answer is -1.

Source: codechef | Difficulty: MEDIUM


Solutions:
for i in range(int(input())):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	(dict1, ans, zero) = ({}, 0, 0)
	for j in range(n):
		dict1[lst[j]] = 0
	for j in range(n):
		dict1[lst[j]] += 1
	for key in dict1:
		if dict1[key] == k:
			ans += key
			if key == 0:
				zero = 1
	if zero == 1 and ans == 0:
		print(0)
	elif ans > 0:
		print(ans)
	else:
		print(-1)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = sorted(list(map(int, input().split())))
	krepeat = []
	f = 1
	for i in range(1, n):
		if l[i] == l[i - 1]:
			f += 1
		else:
			if f == k:
				krepeat.append(l[i - 1])
			f = 1
	if f == k:
		krepeat.append(l[-1])
	print(sum(krepeat) if len(krepeat) else -1)


from collections import Counter
t = int(input())
while t > 0:
	flag = 0
	x = 0
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter(a)
	for i in c:
		if k == c[i]:
			flag = 1
			x += i
	if flag:
		print(x)
	else:
		print(-1)
	t = t - 1


import os, sys, io, math
from re import *
from math import *
from array import *
from heapq import *
from bisect import *
from functools import *
from itertools import *
from statistics import *
from collections import *
I = lambda : [*map(int, sys.stdin.readline().split())]
IP = lambda : map(int, input().split())
IS = lambda : input()
IN = lambda : int(input())
IF = lambda : float(input())

def h():
	(n, k) = IP()
	l = I()
	res = 0
	d = Counter(l)
	ok = False
	for i in d:
		if d[i] == k:
			res += i
			ok = True
	return res if ok else -1
for z in range(IN()):
	print(h())


t = int(input())
res = []
while t > 0:
	(n, k) = input().split()
	(n, k) = (int(n), int(k))
	reps = {}
	l = []
	l = input().split()
	l = [eval(i) for i in l]
	l = sorted(l)
	for i in range(len(l)):
		if l[i] not in reps.keys():
			reps[l[i]] = 0
			reps[l[i]] += 1
		else:
			reps[l[i]] += 1
	sum = 0
	sum = int(sum)
	reps_found = 0
	for key in reps.keys():
		if reps[key] == k:
			sum += key
			reps_found = 1
	if reps_found == 0:
		res.append(-1)
	else:
		res.append(sum)
	t -= 1
for ele in res:
	print(ele)


t = int(input())
from collections import Counter
for _ in range(t):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	clist = Counter(lst)
	flag = 0
	x = 0
	for i in clist:
		vl = clist[i]
		if vl == k:
			flag = 1
			x += i
	if flag:
		print(x)
	else:
		print(-1)


import collections
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = collections.Counter(a)
	s = set(a)
	sum1 = 0
	flag = 0
	for i in s:
		if d[i] == k:
			sum1 += i
			if i == 0:
				flag = 1
	if flag:
		print(sum1)
	elif sum1 == 0:
		print(-1)
	else:
		print(sum1)


from collections import Counter
for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	d = Counter(a)
	s = 0
	f = 0
	for i in d:
		if d[i] == k:
			s += i
			f = 1
	if f == 0:
		print(-1)
	else:
		print(s)


import sys
for i in range(int(sys.stdin.readline())):
	(n, m) = map(int, sys.stdin.readline().split())
	ar = list(map(int, sys.stdin.readline().split()))
	d = {}
	for j in range(n):
		x = ar[j]
		if d.get(x) == None:
			d[x] = 0
		d[x] += 1
	br = list(d.keys())
	ans = 0
	hit = False
	for u in range(len(br)):
		if d[br[u]] == m:
			ans += br[u]
			hit = True
	if not hit:
		print(-1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	d = {}
	for i in A:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	total = 0
	zero = 0
	for i in d:
		if d[i] == K:
			total = total + i
			if i == 0:
				zero = 1
	if total == 0:
		if zero == 0:
			print(-1)
		else:
			print(0)
	else:
		print(total)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	li = list(map(int, input().split()))
	dict = {}
	for i in li:
		if i not in dict:
			dict[i] = 0
		dict[i] += 1
	sum = 0
	flag = True
	for (i, j) in dict.items():
		if j == b:
			flag = False
			sum += i
	if flag:
		print(-1)
	else:
		print(sum)


t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	dict = {}
	for x in arr:
		if x in dict:
			dict[x] += 1
		else:
			dict[x] = 1
	result = 0
	satified = False
	for x in dict:
		if dict[x] == k:
			satified = True
			result += x
	if result == 0:
		if satified:
			print(result)
		else:
			print(-1)
	else:
		print(result)


tc = int(input())
for _ in range(tc):
	(n, k) = [int(x) for x in input().split()]
	arr = [int(x) for x in input().split()]
	dict = {}
	for x in arr:
		if x in dict:
			dict[x] += 1
		else:
			dict[x] = 1
	result = 0
	satified = False
	for x in dict:
		if dict[x] == k:
			satified = True
			result += x
	if result == 0:
		if satified:
			print(result)
		else:
			print(-1)
	else:
		print(result)


from collections import Counter
t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	p = [int(ele) for ele in input().strip().split()][:n]
	d = dict(Counter(p))
	s = 0
	for i in d:
		if d[i] == k:
			s += i
	if s == 0 and 0 not in p:
		print(-1)
	elif s == 0 and 0 in p and (d[0] != k):
		print(-1)
	else:
		print(s)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	(ans, flag) = (0, 0)
	list1 = [int(x) for x in input().split()]
	list2 = []
	dict = {}
	for i in range(n):
		if list1[i] not in dict.keys():
			dict[list1[i]] = 1
		else:
			dict[list1[i]] += 1
	for key in dict:
		if dict[key] == k:
			ans += key
			flag = 1
	if flag == 0:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	count = 0
	virginity = 0
	list1 = list(map(int, input().split()))
	list2 = []
	dict = {}
	for i in range(n):
		if list1[i] not in dict.keys():
			dict[list1[i]] = 1
		else:
			dict[list1[i]] = dict[list1[i]] + 1
	for i in dict:
		if dict.get(i) == k:
			count = count + i
			virginity = 1
	if virginity == 0:
		count = -1
	print(count)


from collections import Counter
for i in range(int(input())):
	(a, b) = map(int, input().split())
	l = list(map(int, input().split()))
	c = Counter(l)
	ans = 0
	det = 0
	for i in c:
		if c[i] == b:
			ans += i
			det = 1
	if not ans and (not det):
		print(-1)
	else:
		print(ans)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	N = list(map(int, input().split()))
	c = Counter(N)
	ans = 0
	det = 0
	for i in c:
		if c[i] == k:
			ans += i
			det = 1
	if not ans and (not det):
		print(-1)
	else:
		print(ans)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split(' '))
	arr = input().split(' ')
	arr = [int(x) for x in arr]
	freq = {}
	for key in arr:
		if key in freq:
			val = freq[key]
			freq.update({key: val + 1})
		else:
			freq[key] = 1
	ans = 0
	flag = 0
	for (key, val) in freq.items():
		if val == k:
			flag = 1
			ans += key
	if ans == 0 and flag == 0:
		print(-1)
	else:
		print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split(' '))
	arr = list(map(int, input().split(' ')))
	d = {}
	for i in arr:
		try:
			d[i] += 1
		except:
			d[i] = 1
	sum = 0
	flag = True
	for i in d.keys():
		if d[i] == k:
			sum += i
			flag = False
	if flag:
		sum = -1
	print(sum)


for _ in range(int(input())):
	mydict = {}
	(n, k) = map(int, input().split())
	for i in map(int, input().split()):
		if i not in mydict:
			mydict[i] = 0
		mydict[i] += 1
	val = 0
	null = False
	for i in mydict:
		if mydict[i] == k:
			val += i
			null = True
	if null == False:
		print(-1)
	else:
		print(val)


import collections
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	c = collections.Counter(l)
	sum = 0
	z = 1
	for i in c:
		if c[i] == k:
			sum += i
			z = 0
	if sum == 0 and z == 1:
		print(-1)
	else:
		print(sum)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	key = []
	value = []
	dic = {}
	ans = 0
	flag = 1
	for i in a:
		if i not in dic:
			dic[i] = 1
		else:
			dic[i] += 1
	for i in dic.keys():
		key.append(i)
	for i in dic.values():
		value.append(i)
	for (i, j) in enumerate(value):
		if j == k:
			ans += key[i]
			flag = 0
	if ans == 0 and flag == 1:
		print(-1)
	else:
		print(ans)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	flag = 1
	for (i, j) in Counter(a).items():
		if j == k:
			ans += i
			flag = 0
	if ans == 0 and flag == 1:
		print(-1)
	else:
		print(ans)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter(a).items()
	s = 0
	f = False
	for (b, v) in c:
		if v == k:
			s += b
			f = True
	if s == 0 and f == False:
		print(-1)
	else:
		print(s)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	c = Counter(arr)
	s = -1
	f = 0
	for i in c:
		if c[i] == k:
			f = 1
			s += i
	if f == 0:
		print(-1)
	else:
		print(s + 1)


for x in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	temp_dict = {}
	sum = 0
	check = 0
	for x in l:
		if x not in temp_dict.keys():
			temp_dict[x] = 1
		else:
			temp_dict[x] += 1
	for x in temp_dict.keys():
		if temp_dict[x] == k:
			sum = sum + x
			check = 111
	if check == 111:
		print(sum)
	else:
		print(-1)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	list1 = list(map(int, input().split()))
	dict1 = {}
	sum1 = 0
	okay = 0
	for j in list1:
		if j not in dict1.keys():
			dict1[j] = 1
		else:
			dict1[j] += 1
	for s in dict1.keys():
		if dict1[s] == k:
			okay = 1
			sum1 += s
	if okay == 1:
		print(sum1)
	else:
		print(-1)


test = int(input())
for t in range(test):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	num_sum = 0
	no_sums = True
	itterated_nums = {}
	for i in A:
		if i not in itterated_nums:
			itterated_nums[i] = 0
		itterated_nums[i] += 1
	for s in itterated_nums:
		if itterated_nums.get(s) == K:
			num_sum += s
			no_sums = False
	if no_sums:
		print(-1)
	else:
		print(num_sum)


for t in range(int(input())):
	(n, k) = list(map(int, input().split()))
	li = list(map(int, input().split()))
	dict = {}
	(res_sum, flag) = (0, 0)
	for item in li:
		if item not in dict.keys():
			dict[item] = 1
		else:
			dict[item] += 1
	for a in dict.keys():
		if dict[a] == k:
			res_sum += a
			flag = 1
	if flag == 0:
		print('-1')
	else:
		print(res_sum)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		try:
			d[i] += 1
		except:
			d[i] = 1
	s = 0
	c = 0
	for i in d.keys():
		if d[i] == k:
			s += i
			c = 1
	print(s if c == 1 else -1)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	count = {}
	for i in range(n):
		if a[i] in count:
			count[a[i]] += 1
		else:
			count[a[i]] = 1
	s = 0
	bl = False
	for i in count:
		if count[i] == k:
			s += i
			bl = True
	if bl:
		print(s)
	else:
		print(-1)


from collections import Counter
for _ in range(int(input())):
	s = 0
	c = 0
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = Counter(l)
	for i in d.keys():
		if k == d[i]:
			s = s + i
			c = 1
	if c == 1:
		print(s)
	else:
		print(-1)


tests = int(input())
for i in range(tests):
	(length, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	freq = {}
	ans = 0
	for j in arr:
		if j in freq:
			freq[j] += 1
		else:
			freq[j] = 1
	for j in freq.keys():
		if freq[j] == k:
			ans += j
	if ans == 0 and (0 not in freq or freq[0] != k):
		ans = -1
	print(ans)


t = int(input())
for i in range(t):
	nk = input().split()
	n = int(nk[0])
	k = int(nk[1])
	ar = input().split()
	arr = [int(l) for l in ar]
	freq = {}
	for ele in arr:
		if ele not in freq:
			freq[ele] = 1
		else:
			freq[ele] += 1
	su = 0
	fl = 0
	for key in freq:
		if freq[key] == k:
			fl = 1
			su += key
	if fl:
		print(su)
	else:
		print(-1)


T = int(input())
while T > 0:
	(n, k) = map(int, input().split())
	lst = [int(i) for i in input().split()]
	counter = dict()
	s = 0
	count = 0
	for i in lst:
		if i in counter:
			counter[i] += 1
		else:
			counter[i] = 1
		if counter[i] == k:
			s += i
			count += 1
		if counter[i] == k + 1:
			s -= i
			count -= 1
	if count == 0:
		print(-1)
	else:
		print(s)
	T -= 1


T = int(input())
while T > 0:
	(n, k) = map(int, input().split())
	lst = [int(i) for i in input().split()]
	counter = dict()
	s = 0
	for i in lst:
		if i in counter:
			counter[i] += 1
		else:
			counter[i] = 1
	flag = False
	for (i, j) in counter.items():
		if j == k:
			s += i
			flag = True
	if flag:
		print(s)
	else:
		print(-1)
	T -= 1


T = int(input())
for i in range(T):
	(N, K) = map(int, input().split(' '))
	A = list(map(int, input().split(' ')))
	D = {}
	for j in A:
		if j in D:
			D[j] += 1
		else:
			D[j] = 1
	s = 0
	exist = False
	for j in D:
		if D[j] == K:
			s += j
			exist = True
	if exist == False:
		s = -1
	print(s)


from collections import Counter
testCases = int(input())
for testCase in range(testCases):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	sum = 0
	flag = False
	for i in Counter(a).most_common():
		if i[1] == k:
			sum += i[0]
			flag = True
	if flag:
		print(sum)
	else:
		print(-1)


from collections import Counter

def list_to_frequency_dictionary(input_list):
	return dict(Counter(input_list))

def solver(frequency_dictionary, k):
	no_match = True
	output = 0
	for (key, value) in frequency_dictionary.items():
		if value == k:
			output += key
			no_match = False
	if no_match:
		return -1
	return output
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	input_list = list(map(int, input().split()))
	fr_dict = list_to_frequency_dictionary(input_list)
	print(solver(fr_dict, k))


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	t -= 1
	d = {}
	sum = 0
	z = 0
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	for i in range(0, len(d)):
		if list(d.values())[i] == k:
			sum += list(d.keys())[i]
			z = 1
	if z == 0:
		print(-1)
	else:
		print(sum)


from collections import Counter
for i in range(int(input())):
	(N, K) = map(int, input().split())
	l = list(map(int, input().split()))
	d = dict(Counter(l))
	sum = 0
	ans = -1
	for (i, j) in d.items():
		if j == K:
			sum = sum + i
			ans = sum
	print(ans)


t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	ls = list(map(int, input().split()))
	ls.sort()
	(count, nsum) = (1, -1)
	if n == 1:
		print(ls[0])
	else:
		for i in range(n - 1):
			if ls[i] == ls[i + 1]:
				count += 1
			else:
				if count == k:
					if nsum == -1:
						nsum = 0
					nsum += ls[i]
				count = 1
		if count == k:
			if nsum == -1:
				nsum = 0
			nsum += ls[n - 1]
		if nsum == -1:
			print(-1)
		else:
			print(nsum)


from collections import Counter
for i in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	l = list(map(int, input().split()))
	d = Counter(l)
	s = 0
	f = 0
	for i in d:
		if d[i] == k:
			s += i
			f = 1
	if f == 0:
		print(-1)
	else:
		print(int(s))


from collections import Counter
for x in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter(a)
	ans = 0
	f = 0
	for xx in c:
		if c[xx] == k:
			ans += xx
			f += 1
	if f == 0:
		print(-1)
	else:
		print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().lstrip().split()))
	sum = 0
	di = dict()
	Flag = False
	for i in range(n):
		if arr[i] in di:
			di[arr[i]] += 1
		else:
			di[arr[i]] = 1
	for j in di:
		if di[j] == k:
			sum += j
		if di[j] == k and j == 0:
			Flag = True
	if sum == 0 and Flag == False:
		print(-1)
	elif sum == 0 and Flag == True:
		print(0)
	else:
		print(sum)


from collections import Counter
t = int(input())
while t > 0:
	t -= 1
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = dict(Counter(l))
	s = 0
	if k not in d.values():
		print(-1)
		continue
	for i in d:
		if d[i] == k:
			s += i
	print(s)


T = int(input())
for i in range(0, T):
	(a, b) = input().split()
	a = int(a)
	b = int(b)
	li = {}
	bb = input().split()
	for j in bb:
		li[j] = 0
	for j in bb:
		li[j] += 1
	ans = 0
	doit = 0
	for j in li:
		if li[j] == b:
			ans += int(j)
			doit += 1
	if doit == 0:
		print(-1)
	else:
		print(ans)


from collections import Counter
for T in range(int(input())):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	val = Counter(A)
	result = 0
	pin = False
	for i in val:
		if val[i] == K:
			pin = True
			result += i
	if pin is False:
		print(-1)
	else:
		print(result)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	s = Counter(arr)
	ans = 0
	flag = False
	for key in s:
		if s[key] == k:
			flag = True
			ans += key
	if not flag:
		print(-1)
	else:
		print(ans)


def solve():
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	m = {i: 0 for i in arr}
	for i in arr:
		m[i] += 1
	ans = 0
	flag = False
	for i in m:
		if m[i] == k:
			flag = True
			ans += i
	if not flag:
		return -1
	return ans
tc = int(input())
for t in range(tc):
	print(solve())


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for j in l:
		if j in d:
			d[j] += 1
		else:
			d[j] = 1
	r = []
	for (keys, values) in d.items():
		if values == k:
			r.append(keys)
	print(-1 if len(r) == 0 else sum(r))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	dic = {}
	for i in l:
		if i in dic:
			dic[i] += 1
		else:
			dic[i] = 1
	res = []
	for (keys, values) in dic.items():
		if values == k:
			res.append(keys)
	if len(res) == 0:
		print(-1)
	else:
		print(sum(res))


from math import sqrt, ceil, floor, gcd, log2
import sys
from sys import stdin, stdout
from collections import Counter, defaultdict, OrderedDict, deque
from itertools import combinations, groupby
from time import time
from heapq import heappop, heappush, heapify
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	d = dict(Counter(arr))
	ans = 0
	flag = 0
	for (key, val) in d.items():
		if val == k:
			flag = 1
			ans += int(key)
	if ans == 0 and (not flag):
		print(-1)
	else:
		print(ans)


test = int(input())
for i in range(test):
	ans = {}
	(s, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for j in arr:
		if j not in ans:
			ans[j] = 1
		else:
			ans[j] += 1
	s1 = 0
	a = 1
	for j in ans.items():
		if j[1] == k:
			a = 0
			s1 += j[0]
	if a == 1:
		print(-1)
	else:
		print(s1)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	vals = list(map(int, input().split()))
	freq = dict()
	for val in vals:
		freq[val] = freq.get(val, 0) + 1
	res = -1
	for (key, val) in freq.items():
		if val == k:
			if res == -1:
				res = 0
			res += key
	print(res)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = Counter(l)
	sum = 0
	c = 0
	for (key, value) in d.items():
		if k not in d.values():
			c = 1
			break
		if value == k:
			sum += key
	if sum == 0 and c == 1:
		print('-1')
	else:
		print(sum)


from collections import Counter
for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for j in l:
		if j in d:
			d[j] += 1
		else:
			d[j] = 1
	flag = 1
	sum1 = 0
	for (key, value) in d.items():
		if value == k:
			sum1 += key
			flag = 0
	if flag:
		print(-1)
	else:
		print(sum1)


from collections import Counter
for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = dict(Counter(l))
	count = 0
	for i in d.keys():
		if d[i] == k:
			count += i
	if count > 0:
		print(count)
	elif 0 in d.keys() and d[0] == k:
		print(0)
	else:
		print(-1)


t = int(input())
while t > 0:
	t -= 1
	ip = input().split()
	(n, k) = [int(x) for x in ip]
	ip = input().split()
	a = [int(x) for x in ip]
	cnt = {}
	for x in a:
		if x in cnt:
			cnt[x] += 1
		else:
			cnt[x] = 1
	s = 0
	flg = 1
	for i in cnt:
		if cnt[i] == k:
			s += i
			flg = 0
	if flg == 1:
		print(-1)
	else:
		print(s)


for _ in range(int(input())):
	(n, a) = map(int, input().split())
	l = list(map(int, input().split()))
	d = {}
	for i in l:
		try:
			d[i] += 1
		except:
			d[i] = 1
	s = 0
	k = 1
	for i in d:
		if d[i] == a:
			s += i
			k = 2
	if k == 1:
		print(-1)
		continue
	print(s)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	d = {}
	for i in a:
		if i not in d:
			d[i] = 1
		else:
			d[i] += 1
	ans = 0
	f = 0
	for (i, j) in d.items():
		if j == k:
			ans += i
			f = 1
	if f == 0:
		print(-1)
	else:
		print(ans)


from collections import Counter
for _ in range(int(input())):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	D = Counter(A)
	ans = 0
	found = False
	for d in D:
		if D[d] == K:
			ans += d
			found = True
	if found:
		print(ans)
	else:
		print(-1)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	l = [int(e) for e in input().split()]
	d = {}
	for element in l:
		if element in d.keys():
			d[element] += 1
		else:
			d[element] = 1
	total = 0
	check = False
	for key in d.keys():
		if d[key] == k:
			total += key
			check = True
	if check:
		print(total)
	else:
		print('-1')


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	list1 = list(map(int, input().split()))
	counts = {}
	list2 = []
	for i in list1:
		counts[i] = counts.get(i, 0) + 1
	for i in counts:
		if counts[i] == k:
			list2.append(i)
	try:
		x = list2[0]
		print(sum(list2))
	except:
		print(-1)


from collections import Counter
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	hashmap = Counter(a)
	f = False
	ans = 0
	for i in hashmap:
		if hashmap[i] == k:
			ans += i
			f = True
	if f:
		print(ans)
	else:
		print(-1)


import sys
input = sys.stdin.readline

def rii():
	return range(int(input().strip()))

def ii():
	return int(input().strip())

def mii():
	return map(int, input().strip().split(' '))

def lmii():
	return list(map(int, input().strip().split(' ')))

def si():
	return str(input().strip())
from collections import Counter
failure = -1

def solve(*args):
	(n, k, a) = args
	c = Counter(a)
	ans = [x for x in c if c[x] == k]
	return sum(ans) if len(ans) > 0 else failure

def do_codechef():
	for t in rii():
		(n, k) = mii()
		a = lmii()
		print(solve(n, k, a))
do_codechef()
exit()


def ans(arr, k):
	x = set(arr)
	dict = {}
	for y in x:
		dict[y] = 0
	for y in arr:
		dict[y] += 1
	summation = False
	for y in dict.keys():
		if dict[y] == k:
			if summation is False:
				summation = 0
			summation += y
	if summation is False:
		return -1
	return summation
test_cases = int(input())
while test_cases != 0:
	data = list(map(int, input().split()))
	data2 = list(map(int, input().split()))
	print(ans(data2, data[1]))
	test_cases -= 1