Akash got his money from CodeChef today, so he decided to have dinner outside.  
He went to a restaurant having N items on the menu. The i^{th} item on the menu belongs to the *category* A_{i} and requires B_{i} time to be cooked.

Akash wants to have a *complete meal*. Thus, his meal should have at least K distinct *categories* of food.  
The total time required to get all the food Akash orders, is the sum of the cooking time of all the items in the order.

Help Akash find the minimum time required to have a complete meal or tell if it is not possible to do so.

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

- First line will contain T, the number of test cases. Then the test cases follow.
- Each test case contains three lines:
- The first line of each test case contains two space-separated integers N and K, denoting the number of dishes on the menu and the number of distinct categories in a complete meal.
- The second line contains N space-separated integers where the i^{th} integer is A_{i}, denoting the category of the i^{th} dish in the menu.
- The third line contains N space-separated integers where the i^{th} integer is B_{i}, denoting the time required to cook the i^{th} dish in the menu.

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

For each test case, output in a single line, the minimum time required to have a complete meal.

If it is impossible to have a complete meal, print -1 instead.

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

$1 ≤ T ≤ 100$
$1 ≤ N,K ≤ 10^{5}$
$1 ≤ A_{i} ≤ 10^{5}$
$0 ≤ B_{i} ≤ 10^{5}$
- The sum of $N$ over all test cases won't exceed $10^{5}$.

----- Sample Input 1 ------ 
4
3 1
1 2 3
2 1 3
8 3
1 3 2 2 4 1 3 5
3 3 0 1 2 4 1 4
1 1
5
1
5 3
1 1 2 2 1
1 1 0 3 5
----- Sample Output 1 ------ 
1
3
1
-1
----- explanation 1 ------ 
Test case $1$: Akash can choose dish with index $2$ having category $2$. The total time required to get the complete meal is $1$.

Test case $2$: Akash can choose dishes with index $3, 5,$ and $7$ from the menu.
- Dish $3$: The dish has category $2$ and requires time $0$.
- Dish $5$: The dish has category $4$ and requires time $2$.
- Dish $7$: The dish has category $3$ and requires time $1$.

Thus, there are $3$ distinct categories and the total time to get the meal is $0+2+1 = 3$. It can be shown that this is the minimum time to get the *complete meal*.

Test case $3$: Akash can choose the only available dish having category $5$. The total time required to get the complete meal is $1$.

Test case $4$: The total number of distinct categories available is $2$, which is less than $K$. Thus, it is impossible to have a *complete* meal.

Difficulty: MEDIUM
Tags: Sorting
Skill types: Sorting

Starter code:

Solutions:
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = b[i]
		else:
			d[a[i]] = min(b[i], d[a[i]])
	if len(d) < k:
		print(-1)
	else:
		array = []
		for dd in d:
			array.append(d[dd])
		array.sort()
		sum = 0
		for i in range(k):
			sum += array[i]
		print(sum)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	if len(set(a)) < k:
		print(-1)
	else:
		d = {}
		for i in range(n):
			d[a[i]] = min(d.get(a[i], 10 ** 5), b[i])
		v = list(d.values())
		v.sort()
		print(sum(v[:k]))


for tc in range(int(input())):
	(n, kdish) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	kmap = {}
	for i in range(n):
		if a[i] not in kmap:
			kmap[a[i]] = b[i]
		else:
			kmap[a[i]] = min(b[i], kmap[a[i]])
	if len(kmap) < kdish:
		print(-1)
	else:
		arr = []
		for k in kmap:
			arr.append(kmap[k])
		arr.sort()
		tot = 0
		for i in range(kdish):
			tot += arr[i]
		print(tot)


t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	category = list(map(int, input().split()))
	food_time = list(map(int, input().split()))
	food = {}
	for j in range(n):
		if category[j] in food:
			Previous_time = food[category[j]]
			if Previous_time > food_time[j]:
				food[category[j]] = food_time[j]
		else:
			food[category[j]] = food_time[j]
	if len(food) >= k:
		min_time = sorted(list(food.values()))
		print(sum(min_time[0:k]))
	else:
		print('-1')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if d.get(a[i], -1) == -1:
			d[a[i]] = b[i]
		else:
			d[a[i]] = min(d.get(a[i], 0), b[i])
	m = len(d)
	if k > m:
		print(-1)
	else:
		ans = sum(list(sorted(d.values())[:k]))
		print(ans)


from collections import defaultdict
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	cat = defaultdict(lambda : 1000000)
	for i in range(n):
		cat[a[i]] = min(cat[a[i]], b[i])
	times = list(cat.values())
	if len(times) < k:
		print(-1)
		continue
	times.sort()
	total = 0
	for i in range(k):
		total += times[i]
	print(total)


t = int(input())
for i in range(0, t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	time = {}
	for j in range(0, n):
		if a[j] not in time:
			time.update({a[j]: b[j]})
		else:
			time[a[j]] = b[j] if b[j] < time[a[j]] else time[a[j]]
	if len(time) >= k:
		times = list(time.values())
		times.sort()
		print(sum(times[0:k]))
	else:
		print(-1)


for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	a = list(map(int, input().split(' ')))
	b = list(map(int, input().split(' ')))
	res = {}
	for i in range(n):
		if res.get(a[i], -1) == -1:
			res[a[i]] = b[i]
		else:
			res[a[i]] = min(res.get(a[i], 0), b[i])
	l = len(res)
	if k > l:
		print(-1)
	else:
		ans = sum(list(sorted(res.values())[:k]))
		print(ans)


t = int(input())
for i in range(t):
	n_k = input().split(' ')
	category = input().split(' ')
	time = input().split(' ')
	s_cat = set(category)
	if len(s_cat) < int(n_k[1]):
		print(-1)
	else:
		l_time = {}
		for j in range(int(n_k[0])):
			if category[j] not in l_time:
				l_time.update({category[j]: int(time[j])})
			elif category[j] in l_time and l_time.get(category[j]) > int(time[j]):
				l_time.update({category[j]: int(time[j])})
		l = list(l_time.values())
		l.sort()
		print(sum(l[0:int(n_k[1])]))


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	clist = list(map(int, input().split()))
	tlist = list(map(int, input().split()))
	st = set(clist)
	lnst = len(st)
	if lnst < k:
		print(-1)
		continue
	xlist = {}
	for i in range(n):
		cv = clist[i]
		tv = tlist[i]
		if cv in xlist:
			xlist[cv] = min(xlist[cv], tv)
		else:
			xlist[cv] = tv
	val = list(xlist.values())
	val.sort()
	print(sum(val[:k]))


t = int(input())
for t in range(t):
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	ans = {}
	for i in range(n):
		if a[i] not in ans:
			ans[a[i]] = b[i]
		elif ans[a[i]] > b[i]:
			ans[a[i]] = b[i]
	if len(ans) < k:
		print(-1)
	else:
		arr = sorted(ans.values())
		s = len(arr) - k
		for i in range(s):
			arr.remove(arr[len(arr) - 1])
		time = sum(arr)
		print(time)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	items = set()
	time = 0
	total = 0
	a = [A for (B, A) in sorted(zip(b, a))]
	b.sort()
	for i in range(n):
		if a[i] not in items:
			items.add(a[i])
			time += b[i]
			total += 1
		if total == k:
			break
	if total == k:
		print(time)
	else:
		print(-1)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if len(set(a)) < k:
		print(-1)
	else:
		d = {}
		for i in range(n):
			if a[i] not in d:
				d[a[i]] = b[i]
			else:
				d[a[i]] = min(d[a[i]], b[i])
		c = []
		for i in d.values():
			c.append(i)
		c.sort()
		print(sum(c[:k]))


import numpy as np
foodtime = [('food', int), ('time', int)]
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	foodlist = list(map(int, input().split()))
	timelist = list(map(int, input().split()))
	menu = {}
	for i in range(n):
		menu.update({foodlist[i]: -1})
	for i in range(n):
		if timelist[i] < menu[foodlist[i]] or menu[foodlist[i]] == -1:
			menu.update({foodlist[i]: timelist[i]})
	v = sorted(menu.values())
	if len(v) >= m:
		print(sum(v[:m]))
	else:
		print(-1)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = dict()
	for i in range(n):
		if a[i] in d.keys():
			d[a[i]] = min([d[a[i]], b[i]])
		else:
			d[a[i]] = b[i]
	if len(d.keys()) < k:
		print(-1)
		continue
	outs = sorted([i for i in d.values()])
	print(sum(outs[:k]))


def dinner(n, k, a, b):
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]].append(b[i])
		else:
			d[a[i]] = [b[i]]
	c = set(a)
	if len(c) < k:
		return -1
	else:
		s = 0
		exi = []
		for i in d:
			mini = min(d[i])
			exi.append(mini)
		exi.sort()
		return sum(exi[:k])
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = dinner(n, k, a, b)
	print(ans)


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


for i in range(int(input())):
	(Num, K) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	dt = {}
	if len(set(A)) < K:
		print(-1)
	else:
		for i in range(len(A)):
			if A[i] not in dt:
				dt[A[i]] = B[i]
			elif dt[A[i]] > B[i]:
				dt[A[i]] = B[i]
		lis = list(dt.values())
		lis.sort()
		Sum = sum(lis[0:K])
		print(Sum)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = set(a)
	time = dict()
	for i in range(n):
		if a[i] not in time:
			time[a[i]] = b[i]
		elif b[i] < time[a[i]]:
			time[a[i]] = b[i]
	if len(s) < k:
		print(-1)
	else:
		l = list(time.values())
		l.sort()
		low = l[:k]
		print(sum(low))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	time = dict()
	for i in range(n):
		if a[i] not in time:
			time[a[i]] = b[i]
		elif b[i] < time[a[i]]:
			time[a[i]] = b[i]
	if len(time) < k:
		print(-1)
	else:
		low = sorted(list(time.values()))[:k]
		print(sum(low))


for t in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]] = min(b[i], d[a[i]])
		else:
			d[a[i]] = b[i]
	if len(d) < k:
		print('-1')
	else:
		v = list(d.values())
		v.sort()
		sum = 0
		for x in range(k):
			sum += v[x]
		print(sum)


import numpy as np
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = np.array(list(map(int, input().split())))
	b = np.array(list(map(int, input().split())))
	idx = b.argsort()
	b = b[idx]
	a = a[idx]
	(av, ai) = np.unique(a, return_index=True)
	if len(av) < k:
		print(-1)
		continue
	b_ = b[ai]
	b_.sort()
	ans = np.sum(b_[:k])
	print(ans)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]] = min(d[a[i]], b[i])
		else:
			d[a[i]] = b[i]
	li = []
	for i in d:
		li.append((i, d[i]))
	if len(li) >= k:
		li.sort(key=lambda x: x[1])
		ans = 0
		for i in range(k):
			ans += li[i][1]
		print(ans)
	else:
		print(-1)


def main():
	tests = int(input())
	for test in range(tests):
		(total, taste) = map(int, input().split())
		a = list(map(int, input().split()))
		b = list(map(int, input().split()))
		dishes = dict()
		for i in range(total):
			if a[i] not in dishes:
				dishes.update({a[i]: b[i]})
			else:
				dishes[a[i]] = b[i] if b[i] < dishes[a[i]] else dishes[a[i]]
		if len(dishes) >= taste:
			time = list(dishes.values())
			time.sort()
			print(sum(time[:taste]))
		else:
			print('-1')
main()


for i in range(int(input())):
	(n, k) = map(int, input().split())
	list1 = list(map(int, input().split()))
	list2 = list(map(int, input().split()))
	dict1 = {}
	j = 0
	while j < n:
		if list1[j] not in dict1.keys():
			dict1[list1[j]] = list2[j]
		elif list2[j] < dict1[list1[j]]:
			dict1[list1[j]] = list2[j]
		j += 1
	if len(dict1.keys()) < k:
		print(-1)
	else:
		p = list(dict1.values())
		p.sort()
		print(sum(p[:k]))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	brr = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if arr[i] in d:
			d[arr[i]] = min(d[arr[i]], brr[i])
		else:
			d[arr[i]] = brr[i]
	if len(d) < k:
		print(-1)
	else:
		temp = list(sorted(d.values()))
		print(sum(temp[:k]))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dic = {}
	temp = max(b)
	for j in a:
		dic[j] = temp
	for j in range(n):
		dic[a[j]] = min(dic[a[j]], b[j])
	if len(dic) < k:
		print(-1)
	else:
		l = [j for j in dic.values()]
		l.sort()
		ans = 0
		for j in range(len(l)):
			if j >= k:
				break
			ans += l[j]
		print(ans)


test = int(input())
for t in range(test):
	(n, k) = map(int, input().split())
	item = list(map(int, input().split()))
	cooktime = list(map(int, input().split()))
	unqitems = dict()
	for i in range(n):
		if item[i] in unqitems:
			unqitems[item[i]] = min(unqitems[item[i]], cooktime[i])
		else:
			unqitems[item[i]] = cooktime[i]
	if len(unqitems) < k:
		print(-1)
	else:
		values = list(sorted(unqitems.values()))
		print(sum(values[:k]))


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	lsta = list(map(int, input().split()))
	lstc = []
	lstb = list(map(int, input().split()))
	dict = {}
	for i in range(n):
		if lsta[i] in dict:
			dict[lsta[i]] = min(lstb[i], dict[lsta[i]])
		else:
			dict[lsta[i]] = lstb[i]
	for (key, value) in dict.items():
		lstc.append(value)
	lstc.sort()
	ans = 0
	if k <= len(lstc):
		for i in range(k):
			ans += lstc[i]
	if len(lstc) < k:
		print(-1)
	else:
		print(ans)


import math
for i in range(int(input())):
	(n, k) = map(int, input().split())
	arr1 = list(map(int, input().split()))
	arr2 = list(map(int, input().split()))
	d = {}
	if k > len(set(arr1)):
		print(-1)
	elif k <= len(set(arr1)):
		for i in range(n):
			d[arr1[i]] = math.inf
		for i in range(n):
			d[arr1[i]] = min(d[arr1[i]], arr2[i])
		d_sorted = sorted(d.items(), key=lambda x: x[1])
		converted_dict = dict(d_sorted)
		val = list(converted_dict.values())
		s = 0
		for i in range(k):
			s += val[i]
		print(s)


t = int(input())
for i in range(t):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	hast = {}
	for i in range(len(A)):
		if A[i] in hast:
			hast[A[i]] = min(hast[A[i]], B[i])
		else:
			hast[A[i]] = B[i]
	L = []
	for i in hast:
		L.append(hast[i])
	L.sort()
	if K > len(L):
		print(-1)
	else:
		print(sum(L[:K]))


def minimum_time(items, k):
	items.sort(key=lambda x: x[1])
	categories = set()
	total_time = 0
	for item in items:
		(category, time) = item
		if category not in categories:
			categories.add(category)
			total_time += time
		if len(categories) >= k:
			return total_time
	return -1
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	items = []
	for i in range(n):
		items.append([a[i], b[i]])
	print(minimum_time(items, k))


def cal(a, b, n, k):
	d = dict()
	for i in range(n):
		if a[i] in d.keys():
			if d.get(a[i]) > b[i]:
				d[a[i]] = b[i]
		else:
			d[a[i]] = b[i]
	l = []
	for i in d.keys():
		l.append([d.get(i), i])
	l.sort()
	size = len(l)
	time = 0
	while size > 0 and k > 0:
		b = l.pop(0)
		size -= 1
		time += b[0]
		k -= 1
	if k != 0:
		return -1
	else:
		return time
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	print(cal(a, b, n, k))


for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	t = 0
	mintf = []
	for i in range(n):
		if a[i] not in d.keys():
			d[a[i]] = [b[i]]
		else:
			d[a[i]].append(b[i])
	for i in d.keys():
		mintf.append(min(d[i]))
	mintf.sort()
	if len(mintf) < k:
		print('-1')
	else:
		for i in range(k):
			t += mintf[i]
		print(t)


n = int(input())
for i in range(n):
	(l, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	aa = dict()
	for i in range(l):
		try:
			aa[a[i]] = min(aa[a[i]], b[i])
		except:
			aa[a[i]] = b[i]
	if len(aa) < k:
		print(-1)
		continue
	aa1 = sorted(aa.items(), key=lambda x: x[1])
	an = 0
	for i in aa1:
		an += i[1]
		k -= 1
		if k == 0:
			break
	print(an)


import heapq

def find_min_time(n, k, categories, times):
	category_time = {}
	for i in range(n):
		if categories[i] not in category_time:
			category_time[categories[i]] = times[i]
		else:
			category_time[categories[i]] = min(category_time[categories[i]], times[i])
	if len(category_time) < k:
		return -1
	min_heap = []
	for time in category_time.values():
		heapq.heappush(min_heap, time)
	total_time = 0
	while k > 0:
		min_time = heapq.heappop(min_heap)
		total_time += min_time
		k -= 1
	return total_time

def main():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		categories = list(map(int, input().split()))
		times = list(map(int, input().split()))
		print(find_min_time(n, k, categories, times))
main()


for i in range(int(input())):
	(a, b) = map(int, input().split())
	x = list(map(int, input().split()))
	y = list(map(int, input().split()))
	z = {}
	q = []
	(c, t) = (0, 0)
	for i in range(a):
		if x[i] not in z:
			z[x[i]] = y[i]
		else:
			z[x[i]] = min(z[x[i]], y[i])
	for i in z.values():
		q.append(i)
	q.sort()
	if len(q) >= b:
		print(sum(q[:b]))
	else:
		print(-1)


def Solve(d, n, k):
	if len(d) < k:
		return -1
	values = list(d.values())
	values.sort()
	ans = 0
	for i in range(k):
		ans = ans + values[i]
	return ans
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] in d:
			d[a[i]] = min(b[i], d[a[i]])
		else:
			d[a[i]] = b[i]
	print(Solve(d, n, k))


T = int(input())
import numpy as np
for i in range(T):
	(N, K) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dict = {}
	if len(np.unique(a)) < K:
		print('-1')
	else:
		for i in range(N):
			if a[i] in dict:
				if dict[a[i]] > b[i]:
					dict[a[i]] = b[i]
			else:
				dict[a[i]] = b[i]
		t = list(dict.values())
		t.sort()
		print(sum(t[:K]))


T = int(input())
import numpy as np
for _ in range(T):
	[n, k] = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dict = {}
	if len(np.unique(a)) < k:
		print(-1)
	else:
		for i in range(n):
			if a[i] in dict:
				if dict[a[i]] > b[i]:
					dict[a[i]] = b[i]
			else:
				dict[a[i]] = b[i]
		t = list(dict.values())
		t.sort()
		print(sum(t[:k]))


def solve():
	(n, k) = map(int, input().split())
	di = dict()
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	for i in range(n):
		if a[i] in di:
			di[a[i]] = min(b[i], di[a[i]])
		else:
			di[a[i]] = b[i]
	val = list(di.values())
	print(sum(sorted(val)[:k]) if len(val) >= k else -1)
for _ in range(int(input())):
	solve()


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	d = {}
	for i in range(len(l1)):
		if l1[i] not in d:
			d[l1[i]] = l2[i]
		elif l2[i] < d[l1[i]]:
			d[l1[i]] = l2[i]
	if len(list(d.keys())) < k:
		print(-1)
	else:
		lst = list(d.values())
		c = 0
		t = 0
		lst = sorted(lst)
		for i in range(k):
			t += lst[i]
		print(t)


T = int(input())
while T:
	T -= 1
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	ki = 0
	for i in range(n):
		try:
			d[a[i]] = min(d[a[i]], b[i])
		except KeyError:
			d[a[i]] = b[i]
			ki += 1
	if ki < k:
		print(-1)
	else:
		l = list(d.values())
		l.sort()
		ans = 0
		i = 0
		while i < k:
			ans += l[i]
			i += 1
		print(ans)


def dinner(category, time, N, K):
	look = list(zip(time, category))
	look.sort()
	distinct = {}
	ans = 0
	for i in range(N):
		if len(distinct) >= K:
			return ans
		if look[i][1] in distinct:
			continue
		else:
			distinct[look[i][1]] = True
			ans += look[i][0]
	if len(distinct) >= K:
		return ans
	return -1
for i in range(int(input())):
	(N, K) = map(int, input().split())
	category = list(map(int, input().split()))
	time = list(map(int, input().split()))
	print(dinner(category, time, N, K))


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	s = set(a)
	if len(s) < k:
		print(-1)
	else:
		d = {}
		for i in range(n):
			if a[i] not in d or d[a[i]] > b[i]:
				d[a[i]] = b[i]
			if i == n - 1:
				arr = list(d.values())
		arr.sort()
		print(sum(arr[0:k]))


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if A[i] in d.keys():
			d[A[i]] = min(d[A[i]], B[i])
		else:
			d[A[i]] = B[i]
	if len(d.keys()) < k:
		print(-1)
		continue
	print(sum(sorted(d.values())[:k]))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = b[i]
		else:
			d[a[i]] = min(d[a[i]], b[i])
	l = list(d.values())
	l = sorted(l)
	if len(l) >= k:
		y = l[:k]
		x = sum(y)
		print(x)
	else:
		print(-1)


try:
	T = int(input())
	for _ in range(T):
		(N, K) = map(int, input().split())
		l1 = list(map(int, input().split()))
		l2 = list(map(int, input().split()))
		D = dict()
		for i in range(N):
			if D.get(l1[i], -1) == -1:
				D[l1[i]] = l2[i]
			else:
				D[l1[i]] = min(D.get(l1[i], 0), l2[i])
		if len(D) < K:
			print('-1')
		else:
			s = sum(sorted(D.values())[:K])
			print(s)
except:
	pass


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = dict()
	if len(set(a)) < k:
		print(-1)
		continue
	for i in range(len(a)):
		d[a[i]] = b[i]
	for i in range(len(a)):
		d[a[i]] = min(b[i], d[a[i]])
	c = 0
	l = 0
	for i in sorted(d.values()):
		if l < k:
			c += i
		l += 1
	print(c)


import pandas as pd
for t in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	if len(set(a)) < k:
		print(-1)
	else:
		df = pd.DataFrame(zip(a, b), columns=['cat', 'prep'])
		gb = df.groupby(['cat'])['prep'].min().sort_values(ascending=True).head(k)
		print(gb.sum())


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	m = list(map(int, input().split()))
	t = list(map(int, input().split()))
	meals = set(m)
	if k > len(meals):
		print(-1)
	else:
		m = [x for (i, x) in sorted(zip(t, m))]
		t.sort()
		time = 0
		total = 0
		for i in range(n):
			if m[i] in meals:
				time += t[i]
				total += 1
				meals.discard(m[i])
			if total == k:
				break
		print(time)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	categories = list(map(int, input().split()))
	times = list(map(int, input().split()))
	min_times = {}
	for j in range(n):
		if categories[j] in min_times:
			min_times[categories[j]] = min(min_times[categories[j]], times[j])
		else:
			min_times[categories[j]] = times[j]
	if len(min_times) < k:
		print(-1)
	else:
		sorted_min_times = sorted(min_times.items(), key=lambda x: x[1])
		result = sum((time for (category, time) in sorted_min_times[:k]))
		print(result)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	catg = list(map(int, input().split()))
	time = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if catg[i] not in d:
			d[catg[i]] = time[i]
		else:
			d[catg[i]] = min(d[catg[i]], time[i])
	values = sorted(list(d.values()))
	if len(values) >= k:
		print(sum(values[:k]))
	else:
		print(-1)


T = int(input())
for i in range(T):
	(N, K) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	d = {}
	for i in range(N):
		if A[i] not in d:
			d[A[i]] = B[i]
		else:
			d[A[i]] = min(d[A[i]], B[i])
	v = list(d.values())
	v.sort()
	if len(v) < K:
		print(-1)
	else:
		print(sum(v[:K]))


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	c = {}
	for i in range(len(a)):
		if a[i] not in c:
			c[a[i]] = b[i]
		else:
			c[a[i]] = min(c[a[i]], b[i])
	if len(c) < k:
		print(-1)
	else:
		c = sorted(c.values())
		x = sum(c[:k])
		print(x)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split(' ')))
	dishes = list(map(int, input().split(' ')))
	uni = set(dishes)
	time = list(map(int, input().split(' ')))
	arr = [1000000] * 1000000
	s = 0
	for x in range(len(dishes)):
		if arr[dishes[x]] > time[x]:
			arr[dishes[x]] = time[x]
	arr.sort()
	for i in range(k):
		s = s + arr[i]
	if k > len(uni):
		print(-1)
	else:
		print(s)


testcases = int(input())
for _ in range(testcases):
	smallest = []
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l3 = list(map(int, input().split()))
	d = dict.fromkeys(l2)
	for j in l2:
		d[j] = list()
	for i in range(l1[0]):
		d[l2[i]].append(l3[i])
	for i in d.keys():
		d[i] = sorted(d[i])
	for i in d.values():
		smallest.append(i[0])
	if len(d.keys()) < l1[1]:
		print(-1)
	else:
		print(sum(sorted(smallest)[:l1[1]]))


from collections import Counter, defaultdict
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		if a[i] not in d:
			d[a[i]] = b[i]
		elif d[a[i]] > b[i]:
			d[a[i]] = b[i]
	if len(d) >= k:
		j = 0
		val = 0
		for i in sorted(d.values()):
			if j == k:
				break
			val += i
			j += 1
		print(val)
	else:
		print(-1)


for t in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	d = {}
	for i in range(n):
		x = d.get(a[i], -1)
		if x == -1:
			d[a[i]] = b[i]
		else:
			d[a[i]] = min(b[i], d.get(a[i], 0))
	if k > len(d):
		print(-1)
		continue
	s = sorted(d.values())
	print(sum(s[:k]))