Petya organized a strange birthday party. He invited $n$ friends and assigned an integer $k_i$ to the $i$-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are $m$ unique presents available, the $j$-th present costs $c_j$ dollars ($1 \le c_1 \le c_2 \le \ldots \le c_m$). It's not allowed to buy a single present more than once.

For the $i$-th friend Petya can either buy them a present $j \le k_i$, which costs $c_j$ dollars, or just give them $c_{k_i}$ dollars directly.

Help Petya determine the minimum total cost of hosting his party.


-----Input-----

The first input line contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.

The first line of each test case contains two integers $n$ and $m$ ($1 \leq n, m \leq 3 \cdot 10^5$) — the number of friends, and the number of unique presents available.

The following line contains $n$ integers $k_1, k_2, \ldots, k_n$ ($1 \leq k_i \leq m$), assigned by Petya to his friends.

The next line contains $m$ integers $c_1, c_2, \ldots, c_m$ ($1 \le c_1 \le c_2 \le \ldots \le c_m \le 10^9$) — the prices of the presents.

It is guaranteed that sum of values $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of values $m$ over all test cases does not exceed $3 \cdot 10^5$.


-----Output-----

For each test case output a single integer — the minimum cost of the party.


-----Examples-----

Input
2
5 4
2 3 4 3 2
3 5 12 20
5 5
5 4 3 2 1
10 40 90 160 250
Output
30
190
Input
1
1 1
1
1
Output
1


-----Note-----

In the first example, there are two test cases. In the first one, Petya has $5$ friends and $4$ available presents. Petya can spend only $30$ dollars if he gives

$5$ dollars to the first friend.

A present that costs $12$ dollars to the second friend.

A present that costs $5$ dollars to the third friend.

A present that costs $3$ dollars to the fourth friend.

$5$ dollars to the fifth friend.

In the second one, Petya has $5$ and $5$ available presents. Petya can spend only $190$ dollars if he gives

A present that costs $10$ dollars to the first friend.

A present that costs $40$ dollars to the second friend.

$90$ dollars to the third friend.

$40$ dollars to the fourth friend.

$10$ dollars to the fifth friend.

Source: codeforces | Difficulty: EASY
Tags: Dynamic programming

Sorting

Amortized analysis

Greedy algorithms


Solutions:
from collections import OrderedDict
for i in range(int(input())):
	(n, m) = map(int, input().split())
	(a, c) = ([*map(int, input().split())], [*map(int, input().split())])
	a = sorted(a)[::-1]
	d = OrderedDict()
	for i in a:
		d[i] = d.get(i, 0) + 1
	j = 0
	cnt = 0
	ans = 0
	for (key, value) in d.items():
		while cnt < key and value > 0:
			cnt += 1
			value -= 1
		ans += value * c[key - 1]
	for i in range(cnt):
		ans += c[i]
	print(ans)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	lstn = list(map(int, input().split()))
	lstm = list(map(int, input().split()))
	lstn.sort(reverse=True)
	pointer = 0
	c = 0
	for j in lstn:
		if pointer <= j:
			a = lstm[j - 1]
			b = lstm[pointer]
			if b < a:
				c += b
				pointer += 1
			else:
				c += a
		else:
			c += lstm[j - 1]
	print(c)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted(list(map(int, input().split())), reverse=True)
	c = list(map(int, input().split()))
	x = 0
	y = 0
	i = 0
	while i < n and y + 1 < k[i] and (y < m):
		x += c[y]
		y += 1
		i += 1
	while i < n:
		x += c[k[i] - 1]
		i += 1
	print(x)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	friends = sorted(list(map(int, input().split())))
	gifts = list(map(int, input().split()))
	initial = n
	ans = 0
	for i in friends:
		if i > initial:
			break
		else:
			ans += gifts[i - 1]
			initial -= 1
	ans += sum(gifts[:initial])
	print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	gc = 0
	cost = 0
	k = [int(i) for i in input().split()]
	c = [int(i) for i in input().split()]
	k = sorted(k)[::-1]
	for i in k:
		if c[gc] < c[i - 1] and gc <= i - 1:
			cost += c[gc]
			gc += 1
		else:
			cost += c[i - 1]
	print(cost)


from math import ceil
for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	cost = list(map(int, input().split()))
	arr.sort(reverse=True)
	Sum = 0
	j = 0
	for i in range(n):
		if cost[j] < cost[arr[i] - 1]:
			Sum += cost[j]
			j += 1
		else:
			Sum += cost[arr[i] - 1]
	print(Sum)


t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split(' ')))
	friends = list(map(int, input().split(' ')))
	presents = list(map(int, input().split(' ')))
	friends.sort(reverse=True)
	res = 0
	i = 0
	for x in friends:
		if x > i + 1:
			res += presents[i]
			i += 1
		else:
			res += presents[x - 1]
	print(res)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	guest = list(map(int, input().split()))
	guest.sort(reverse=True)
	gift = list(map(int, input().split()))
	ans = 0
	j = 0
	for i in guest:
		if j < i:
			ans += gift[j]
			j += 1
		else:
			ans += gift[i - 1]
	print(ans)


t = int(input())
res = ''
for _ in range(t):
	(n, m) = [int(v) for v in input().strip().split(' ')]
	k = [int(v) - 1 for v in input().strip().split(' ')]
	c = [int(v) for v in input().strip().split(' ')]
	k = sorted(k, reverse=True)
	j = 0
	sum = 0
	for i in k:
		if j < i:
			sum += c[j]
			j += 1
		else:
			sum += c[i]
	res = res + '{}\n'.format(sum)
print(res)


ll = lambda : map(int, input().split())
t = lambda : int(input())
ss = lambda : input()
from collections import Counter
from math import inf, ceil
for _ in range(t()):
	(n, m) = ll()
	a = list(ll())
	b = list(ll())
	s1 = 0
	v = [1] * m
	a.sort(reverse=1)
	j = 0
	for i in range(n):
		if b[a[i] - 1] >= b[j] and j <= a[i] - 1:
			s1 += b[j]
			if j + 1 < m:
				j += 1
		else:
			s1 += b[a[i] - 1]
	print(s1)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	l1 = list(map(int, input().split()))
	l2 = list(map(int, input().split()))
	l1.sort(reverse=True)
	sum1 = 0
	for j in range(n):
		if j < m and l1[j] >= j + 1:
			sum1 = sum1 + l2[j]
		else:
			sum1 = sum1 + l2[l1[j] - 1]
	print(sum1)


for _ in '.' * int(input()):
	(n, m) = map(int, input().split())
	integers = list(map(int, input().split()))
	prize = list(map(int, input().split()))
	cost = 0
	pointer = 0
	integers.sort(reverse=True)
	for i in integers:
		i = i - 1
		if prize[i] > prize[pointer]:
			cost += prize[pointer]
			pointer += 1
		else:
			cost += prize[i]
	print(cost)


import copy

def get_result(x: int, curr_index: int):
	res = list_gifts[list_friend[x] - 1]
	if list_gifts[curr_index] < res:
		res = list_gifts[curr_index]
		curr_index += 1
	return (res, curr_index)
t = int(input())
while t > 0:
	total_score = 0
	used_index = 0
	(n, m) = map(int, input().split())
	list_friend = list(map(int, input().split()))
	list_gifts = list(map(int, input().split()))
	list_friend.sort(reverse=True)
	for i in range(0, n):
		(temp_score, used_index) = get_result(i, used_index)
		total_score += temp_score
	print(total_score)
	t -= 1


import io
import os
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop

def solve(N, M, K, C):
	freq = Counter(C)
	heap = []
	for k in K:
		heap.append(-C[k - 1])
	heapify(heap)
	total = -sum(heap)
	for c in sorted(freq.keys()):
		num = freq[c]
		while num > 0 and heap and (c < -heap[0]):
			neg = heappop(heap)
			total += neg
			total += c
			num -= 1
	return total
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
TC = int(input())
for tc in range(1, TC + 1):
	(N, M) = [int(x) for x in input().split()]
	K = [int(x) for x in input().split()]
	C = [int(x) for x in input().split()]
	ans = solve(N, M, K, C)
	print(ans)


import math

def solve(k, g):
	c = [0] * (len(g) + 1)
	for i in range(1, len(c)):
		c[i] = g[i - 1]
	k.sort(reverse=True)
	j = 1
	amt = 0
	for i in k:
		if c[i] <= c[j]:
			amt += c[i]
		else:
			amt += c[j]
			j += 1
	return amt
t = int(input(''))
ans = []
while t > 0:
	n = list(map(int, input('').split(' ')))
	a = list(map(int, input('').split(' ')))
	a2 = list(map(int, input('').split(' ')))
	ans.append(solve(a, a2))
	t -= 1
for i in ans:
	print(i)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	f = list(map(int, input().split()))
	c = list(map(int, input().split()))
	f.sort(reverse=True)
	ans = 0
	for i in range(n):
		if f[i] - 1 >= i and i < m:
			ans += c[i]
		else:
			ans += c[f[i] - 1]
	print(ans)


import math as mt
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	cost = list(map(int, input().split()))
	l.sort(reverse=True)
	j = 1
	total_cost = 0
	for i in range(n):
		if l[i] >= j:
			total_cost += cost[j - 1]
			j += 1
		else:
			total_cost += cost[l[i] - 1]
	print(total_cost)


t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	k = [a for a in map(int, input().split())]
	c = [a for a in map(int, input().split())]
	C = c.copy()
	k.sort()
	k.reverse()
	c.reverse()
	tot = 0
	for a in k:
		if len(c) == 0 or c[-1] >= C[a - 1]:
			tot += C[a - 1]
		else:
			tot += c.pop()
	print(tot)


import math

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b // gcd(a, b)

def calc(q, x):
	ans = 0
	while q % x == 0:
		ans += 1
		q = q // x
	return ans
for _ in range(int(input())):
	(n, x) = map(int, input().split())
	a = list(map(int, input().split()))
	c = list(map(int, input().split()))
	a.sort(reverse=True)
	ans = 0
	j = 0
	for i in range(n):
		if a[i] - 1 > j:
			ans += c[j]
			j += 1
		else:
			ans += c[a[i] - 1]
	print(ans)


from math import *
from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
for _ in range(I()):
	(n, x) = M()
	a = LI()
	b = LI()
	c = sorted(a)
	c.reverse()
	ans = 0
	d = 0
	for i in range(n):
		if b[d] >= b[c[i] - 1]:
			ans += b[c[i] - 1]
		else:
			ans += b[d]
			d += 1
	print(ans)


import sys
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
	(n, m) = map(int, stdin.readline().split())
	k = list(map(int, stdin.readline().split()))
	c = list(map(int, stdin.readline().split()))
	k.sort()
	k.reverse()
	for i in range(n):
		k[i] -= 1
	ind = 0
	ans = 0
	for i in range(n):
		if ind < k[i]:
			ans += c[ind]
			ind += 1
		else:
			ans += c[k[i]]
	print(ans)


import os, sys
from io import BytesIO, IOBase

def main():
	for _ in range(int(input())):
		(n, m) = map(int, input().split())
		k = sorted(map(int, input().split()), reverse=1)
		c = list(map(int, input().split()))
		j = 0
		ans = 0
		for i in range(n):
			if j < k[i] - 1:
				ans += c[j]
				j += 1
			else:
				ans += c[k[i] - 1]
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import math
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort(reverse=True)
	cost = 0
	for i in range(n):
		if i + 1 <= k[i]:
			cost += c[i]
		else:
			cost += c[k[i] - 1]
	print(cost)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	s = sorted(list(map(int, input().split())), reverse=True)
	p = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if i + 1 < s[i]:
			ans += p[i]
		else:
			ans += p[s[i] - 1]
	print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = [int(x) for x in input().split()][:n]
	c = [int(x) for x in input().split()][:m]
	a = c
	sum = 0
	k.sort(reverse=True)
	for i in range(n):
		if k[i] >= i + 1:
			sum += c[i]
		else:
			sum += c[k[i] - 1]
	print(sum)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted(map(int, input().split()), reverse=True)
	c = list(map(int, input().split()))
	cheapest_present = 0
	cost = 0
	for i in range(n):
		if cheapest_present < m:
			if c[cheapest_present] < c[k[i] - 1]:
				cost += c[cheapest_present]
				cheapest_present += 1
			else:
				cost += c[k[i] - 1]
		else:
			cost += c[k[i] - 1]
	print(cost)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = [int(i) for i in input().split()]
	c = [int(i) for i in input().split()]
	a.sort()
	a = a[::-1]
	x = 0
	s = 0
	for i in a:
		if x < i:
			s += c[x]
			x += 1
		else:
			s += c[i - 1]
	print(s)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	c = list(map(int, input().split()))
	look = [0] * m
	for e in a:
		look[e - 1] += 1
	c_asc = [(i, c[i]) for i in range(m)]
	c_asc.sort(key=lambda x: x[1])
	i = 0
	ans = 0
	j = m - 1
	while j >= 0:
		if c_asc[i][0] <= j and c_asc[i][1] < c_asc[j][1] and (look[j] > 0):
			ans += c_asc[i][1]
			i += 1
			look[j] -= 1
			j = j - 1 if look[j] == 0 else j
		else:
			ans += c_asc[j][1] * look[j]
			j -= 1
	print(ans)


def minAmount(n, m, C, K):
	K.sort(reverse=True)
	Amount = 0
	j = 0
	for i in range(n):
		k = K[i] - 1
		if j < m and C[j] < C[k]:
			Amount += C[j]
			j += 1
		else:
			Amount += C[k]
	return Amount
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	K = list(map(int, input().split()))
	C = list(map(int, input().split()))
	print(minAmount(n, m, C, K))


try:
	t = int(input())
	while t != 0:
		(n, m) = map(int, input().split())
		k = list(map(int, input().split()))
		gift = list(map(int, input().split()))
		k.sort(reverse=True)
		mini = 0
		seti = set()
		j = 0
		for i in range(n):
			if j <= k[i] - 1 and j not in seti:
				mini += gift[j]
				seti.add(j)
				j += 1
			else:
				mini += gift[k[i] - 1]
		print(mini)
		t -= 1
except EOFError:
	print(' ')


import sys, os, io
input = sys.stdin.readline
T = int(input())
ans = [0] * T
for t in range(T):
	(N, M) = map(int, input().split())
	K = list(map(int, input().split()))
	C = list(map(int, input().split()))
	C.sort()
	K.sort(reverse=True)
	present = 0
	for k in K:
		if k - 1 > present:
			ans[t] += C[present]
			present += 1
		else:
			ans[t] += C[k - 1]
print(*ans, sep='\n')


import sys, io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
out = []
for _ in range(t):
	out.append(0)
	(n, m) = map(int, input().split())
	l = sorted(map(lambda x: int(x) - 1, input().split()))[::-1]
	c = list(map(int, input().split()))
	curr = 0
	for v in l:
		if curr < m:
			out[-1] += min(c[v], c[curr])
		else:
			out[-1] += c[v]
		curr += 1
print('\n'.join(map(str, out)))


from collections import Counter, defaultdict, deque
import math
from heapq import *
import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7

def solve():
	(a, b) = [int(x) for x in input().split()]
	f = [int(x) for x in input().split()]
	c = [0] + [int(x) for x in input().split()]
	f = sorted(f, reverse=True)
	sum = 0
	p = 1
	for fr in f:
		if c[fr] <= c[p]:
			sum += c[fr]
		else:
			sum += c[p]
			p += 1
	print(sum)
tt = int(input())
for test in range(tt):
	solve()


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k_num = list(map(int, input().split()))
	k_num = sorted(k_num, reverse=True)
	price_gift = list(map(int, input().split()))
	res = 0
	j = 0
	for i in k_num:
		if j < i:
			res += price_gift[j]
			j += 1
		else:
			res += price_gift[i - 1]
	print(res)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	costs = list(map(int, input().split()))
	k.sort(reverse=True)
	dp = [0] * n
	j = 0
	for i in range(n):
		if costs[j] < costs[k[i] - 1]:
			dp[i] = costs[j]
			j += 1
		else:
			dp[i] = costs[k[i] - 1]
	print(sum(dp))


import sys

def answer(n, m, k, c):
	k.sort(reverse=True)
	summ = 0
	cheapest_indx = 0
	for i in range(n):
		if c[cheapest_indx] <= c[k[i] - 1]:
			summ += c[cheapest_indx]
			cheapest_indx += 1
			cheapest_indx = min(cheapest_indx, m - 1)
		else:
			summ += c[k[i] - 1]
	return summ

def main():
	t = int(input())
	while t:
		(n, m) = [int(i) for i in sys.stdin.readline().split()]
		k = [int(i) for i in sys.stdin.readline().split()]
		c = [int(i) for i in sys.stdin.readline().split()]
		print(answer(n, m, k, c))
		t -= 1
	return
main()


import sys
sys.setrecursionlimit(10 ** 6)
fastinput = sys.stdin.readline
fastout = sys.stdout.write
t = int(fastinput())
while t:
	t -= 1
	(n, m) = map(int, fastinput().split())
	k = list(map(int, fastinput().split()))
	c = list(map(int, fastinput().split()))
	k.sort()
	availabe = 0
	result = 0
	while k:
		x = k.pop()
		if availabe < x:
			result += c[availabe]
			availabe += 1
		else:
			result += c[x - 1]
	print(result)


import math
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	c = list(map(int, input().split()))
	l.sort(reverse=True)
	x = 0
	ans = 0
	for i in range(n):
		if c[l[i] - 1] <= c[x] or x == m:
			ans += c[l[i] - 1]
		else:
			ans += c[x]
			x += 1
	print(ans)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.buffer.readline())

def MI():
	return map(int, sys.stdin.buffer.readline().split())

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def LI1():
	return list(map(int1, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def BI():
	return sys.stdin.buffer.readline().rstrip()

def SI():
	return sys.stdin.buffer.readline().rstrip().decode()
inf = 10 ** 16
md = 10 ** 9 + 7
for _ in range(II()):
	(n, m) = MI()
	kk = LI1()
	cc = LI()
	kk.sort()
	mon = [0] * (n + 1)
	for (i, k) in enumerate(kk):
		mon[i + 1] = cc[k]
	for i in range(n):
		mon[i + 1] += mon[i]
	pre = [0]
	for c in cc:
		pre.append(pre[-1] + c)
	ans = inf
	for a in range(max(0, n - m), n + 1):
		cur = mon[a] + pre[n - a]
		if cur < ans:
			ans = cur
	print(ans)


for _ in range(int(input())):
	(n, m) = [int(__) for __ in input().split()]
	k = sorted([int(__) for __ in input().split()], reverse=True)
	c = [int(__) for __ in input().split()]
	shit = []
	for i in k:
		shit.append(c[i - 1])
	for i in range(n):
		if c[i] < shit[i]:
			shit[i] = c[i]
		else:
			break
		if i == m:
			break
	print(sum(shit))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = sorted(list(map(int, input().split())), reverse=True)
	b = list(map(int, input().split()))
	cnt = 0
	x = 0
	for i in range(n):
		if x < m and b[x] <= b[a[i] - 1]:
			cnt += b[x]
			x += 1
		else:
			cnt += b[a[i] - 1]
	print(cnt)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	k = list(map(int, input().split()))
	l = list(map(int, input().split()))
	cost = []
	for e in k:
		cost.append(l[e - 1])
	cost.sort(reverse=True)
	i = 0
	for (e, f) in enumerate(cost):
		if f > l[i]:
			cost[e] = l[i]
			i += 1
	print(sum(cost))


import sys
input = sys.stdin.buffer.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k_sort = [[] for i in range(m)]
	for i in range(n):
		k_sort[k[i] - 1].append(i)
	res = 0
	pos = 0
	for i in range(m - 1, -1, -1):
		for j in k_sort[i]:
			if pos < i:
				res += c[pos]
				pos += 1
			else:
				res += c[i]
	print(res)


test = eval(input())
for _ in range(test):
	(m, n) = map(int, input().split())
	k = list(map(int, input().split()))
	k.sort(reverse=True)
	c = list(map(int, input().split()))
	tCost = 0
	j = 0
	for i in k:
		if c[i - 1] <= c[j]:
			tCost += c[i - 1]
		else:
			tCost += c[j]
			j += 1
	print(tCost)


import math
import sys
import os
if os.path.isfile('C:\\work\\python\\input.txt'):
	sys.stdin = open('C:\\work\\python\\input.txt', 'r')
	sys.stdout = open('C:\\work\\python\\output.txt', 'w')
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	a.sort()
	j = 0
	sm = 0
	for i in range(n - 1, -1, -1):
		if b[j] < b[a[i] - 1]:
			sm += b[j]
			j += 1
		else:
			sm += b[a[i] - 1]
	print(sm)


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
for _ in range(it()):
	(n, m) = mp()
	k = sorted(mp(), reverse=True)
	c = mp()
	ans = 0
	p = 0
	for i in k:
		if p < i:
			ans += c[p]
			p += 1
		else:
			ans += c[i - 1]
	print(ans)


from audioop import reverse

def solve(A, B):
	A.sort(reverse=True)
	s = 0
	m = 0
	for i in range(len(A)):
		if m < len(B) and (m >= A[i] - 1 or B[m] >= B[A[i] - 1]):
			s += B[A[i] - 1]
		elif m >= len(B):
			s += B[A[i] - 1]
		else:
			s += B[m]
			m += 1
	return s
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))[:n]
	b = list(map(int, input().split()))[:m]
	print(solve(a, b))


t = int(input())
for _ in range(t):
	(n, m) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	b = [-1] + [int(i) for i in input().split()]
	a.sort(key=lambda x: -x)
	i = 1
	ans = 0
	for x in a:
		if i > x:
			ans += b[x]
		else:
			ans += b[i]
			i += 1
	print(ans)


for t in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted(list(map(int, input().split())), reverse=True)
	c = list(map(int, input().split()))
	ans = 0
	ind = 0
	for i in range(n):
		if k[i] - 1 < ind:
			ans += c[k[i] - 1]
		else:
			ans += c[ind]
			ind += 1
	print(ans)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort(reverse=True)
	result = 0
	j = 0
	for i in range(n):
		if j < m:
			if c[j] <= c[k[i] - 1]:
				result += c[j]
				j += 1
			else:
				result += c[k[i] - 1]
		else:
			result += c[k[i] - 1]
	print(result)


import sys, io, os
if os.environ['USERNAME'] == 'kissz':
	inp = open('in2.txt', 'r').readline

	def debug(*args):
		print(*args, file=sys.stderr)
else:
	inp = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

	def debug(*args):
		pass
for _ in range(int(inp())):
	(n, m) = map(int, inp().split())
	k = sorted(map(int, inp().split()), reverse=True)
	c = [*map(int, inp().split())]
	i = 0
	s = 0
	for f in k:
		if f - 1 > i:
			s += c[i]
			i += 1
		else:
			s += c[f - 1]
	print(s)


for t in range(int(input())):
	(n, m) = list(map(int, input().split()))
	nums = sorted(list(map(int, input().split())), reverse=True)
	dollar = list(map(int, input().split()))
	given = 0
	cost = 0
	for i in range(n):
		if nums[i] - 1 < given:
			cost += dollar[nums[i] - 1]
		else:
			cost += dollar[given]
			given += 1
	print(cost)


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	assigned = sorted(list(map(int, input().split())), reverse=True)
	presents = list(map(int, input().split()))
	dp = [0 for i in range(m)]
	dp[0] = presents[0]
	taken = 1
	for i in range(1, m):
		if taken < assigned[i]:
			dp[i] = dp[i - 1] + presents[taken]
			taken += 1
		else:
			dp[i] = dp[i - 1] + presents[assigned[i] - 1]
	print(dp[-1])


import sys
from collections import deque

def is_valid(idx_max_gifts):
	return all((i < j for (i, j) in enumerate(idx_max_gifts)))
t = int(sys.stdin.readline())
for _ in range(t):
	(num_friends, num_gifts) = map(int, sys.stdin.readline().split())
	max_gift_idx = sorted(map(int, sys.stdin.readline().split()))
	gift_cost = list(map(int, sys.stdin.readline().split()))
	money_cost = [gift_cost[i - 1] for i in max_gift_idx]

	def is_valid_n(n):
		return is_valid(max_gift_idx[-n:])
	max_num_choices = min(num_friends, num_gifts)
	idx_min = 1
	idx_max = max_num_choices
	while idx_min != idx_max:
		idx_mid = (idx_min + idx_max) // 2
		if idx_min == idx_mid:
			if is_valid_n(idx_max):
				idx_min = idx_max
			else:
				idx_max = idx_min
		elif is_valid_n(idx_mid):
			idx_min = idx_mid
		else:
			idx_max = idx_mid
	total_cost = sum(money_cost)
	min_total_cost = total_cost
	for num_gifts in range(1, idx_max + 1):
		total_cost += gift_cost[num_gifts - 1]
		total_cost -= money_cost[-num_gifts]
		min_total_cost = min(total_cost, min_total_cost)
	print(min_total_cost)


for _ in ' ' * int(input()):
	(n, m) = map(int, input().split())
	a = sorted(map(int, input().split()))[::-1]
	b = list(map(int, input().split()))
	ans = 0
	for i in range(n):
		if a[i] >= i + 1:
			ans += b[i]
		else:
			ans += b[a[i] - 1]
	print(ans)


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	values = list(map(int, input().split()))
	prices = list(map(int, input().split()))
	values = [i - 1 for i in values]
	values.sort(reverse=True)
	(x, res) = (0, 0)
	for i in range(n):
		if values[i] >= x:
			res += prices[x]
			x += 1
		else:
			res += prices[values[i]]
	print(res)


R = lambda : list(map(int, input().split()))
for _ in ' ' * int(input()):
	(n, m) = R()
	k = sorted(R())[::-1]
	c = R()
	sp = 0
	pr = 1
	for i in range(n):
		if k[i] > pr:
			sp += c[pr - 1]
			pr += 1
		else:
			sp += c[k[i] - 1]
	print(sp)


import heapq
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lis = list(map(int, input().split()))
	gifts = list(map(int, input().split()))
	lis.sort()
	lis = lis[::-1]
	heap = []
	heapq.heapify(heap)
	for i in gifts:
		heapq.heappush(heap, i)
	ans = 0
	for i in lis:
		if heap[0] < gifts[i - 1]:
			ans += heapq.heappop(heap)
		else:
			ans += gifts[i - 1]
	print(ans)


t = int(input())
while t:
	(n, m) = map(int, input().split())
	a = list(map(int, input().split(' ')))
	c = list(map(int, input().split(' ')))
	a.sort()
	a.reverse()
	x = -1
	j = 0
	ans = 0
	for i in range(n):
		if j < m:
			if c[j] < c[a[i] - 1]:
				ans += c[j]
				j += 1
			else:
				ans += c[a[i] - 1]
		else:
			ans += c[a[i] - 1]
	print(ans)
	t -= 1


for i in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort(reverse=True)
	b = 0
	for i in range(n):
		if i + 1 <= k[i]:
			b += c[i]
		else:
			b += c[k[i] - 1]
	print(b)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	f = list(map(int, input().split()))
	g = list(map(int, input().split()))
	f = sorted(f, reverse=True)
	t = sorted(g)
	s = 0
	j = 0
	for i in f:
		if g[i - 1] <= t[j]:
			s += g[i - 1]
		else:
			s += t[j]
			j += 1
	print(s)


import sys
import sys
input = sys.stdin.readline
for ad in range(int(input())):
	ans = 0
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort()
	k.reverse()
	z = 0
	for i in range(n):
		if z < k[i]:
			ans += c[z]
			z += 1
		else:
			ans += c[k[i] - 1]
	print(ans)


def solve(n, m, ks, cs):
	if n < m:
		cs = cs[:n]
	gift_i = 0
	cost = 0
	ks.sort(key=lambda x: -1 * x)
	for k in ks:
		if gift_i < k - 1:
			cost += cs[gift_i]
			gift_i += 1
		else:
			cost += cs[k - 1]
	return cost
T = int(input())
for _ in range(T):
	(n, m) = map(int, input().split())
	ks = list(map(int, input().split()))
	cs = list(map(int, input().split()))
	print(solve(n, m, ks, cs))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	K = [int(k) for k in input().split()]
	K.sort(reverse=True)
	C = [int(c) for c in input().split()]
	(s, p) = (0, 0)
	for k in K:
		if k - 1 > p:
			s += C[p]
			p += 1
		else:
			s += C[k - 1]
	print(s)


for _ in range(int(input())):
	(g, d) = input().split()
	a = [int(i) for i in input().split()]
	b = [int(i) for i in input().split()]
	chk = [0] * len(b)
	t = sorted(a, reverse=True)
	s = 0
	j = 0
	for i in range(len(t)):
		f = 0
		if b[j] < b[t[i] - 1]:
			s += b[j]
			j += 1
		else:
			s += b[t[i] - 1]
	print(s)


def solve(n, m, k, c):
	ans = 0
	last = 1
	for i in range(1, n + 1):
		if c[last] < c[k[i]]:
			ans += c[last]
			last = i + 1
		else:
			ans += c[k[i]]
	return ans
for t in range(int(input())):
	(n, m) = map(int, input().split())
	k = [0] + sorted(map(int, input().split()), reverse=True)
	c = [0] + list(map(int, input().split()))
	print(solve(n, m, k, c))


for u in range(int(input())):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	d = list(map(int, input().split()))
	l.sort(reverse=True)
	(s, j) = (0, 0)
	for i in l:
		if j < i:
			s += d[j]
			j += 1
		else:
			s += d[i - 1]
	print(s)


N = int(input())
while N:
	N -= 1
	input()
	a = [int(x) - 1 for x in input().split(' ')]
	b = [int(x) for x in input().split(' ')]
	a = sorted(a, reverse=True)
	res = 0
	j = 0
	for (i, x) in enumerate(a):
		if j == len(b) or b[a[i]] < b[j]:
			res += b[a[i]]
		else:
			res += b[j]
			j += 1
	print(res)


import sys
sys.setrecursionlimit(1000000)
from collections import defaultdict, deque, Counter
from itertools import permutations
from math import sqrt, gcd, pi, factorial
from functools import reduce
from sys import stdin
from bisect import bisect_right, bisect_left
from copy import copy

def solve(n, m, k, c):
	k = sorted(k)
	cnts = [1 for _ in range(m)]
	for i in range(n):
		cnts[k[i] - 1] += 1
	s = 0
	ret = 0
	for i in range(m):
		if s + cnts[i] < n:
			s += cnts[i]
			ret += c[i] * cnts[i]
		else:
			ret += c[i] * (n - s)
			break
	return ret
t = int(input())
anss = [0 for _ in range(t)]
for i in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	anss[i] = solve(n, m, k, c)
for ans in anss:
	print(ans)


test_cases = int(input())
for i in range(test_cases):
	used_presents = 0
	cost = 0
	x = input()
	friends = [int(x) for x in input().split()]
	presents = [int(x) for x in input().split()]
	friends.sort(reverse=True)
	for friend in friends:
		friend -= 1
		if presents[friend] > presents[used_presents]:
			cost += presents[used_presents]
			used_presents += 1
		else:
			cost += presents[friend]
	print(cost)


from os import path
import sys, time
from math import ceil, floor, gcd, log, log2, factorial
from collections import defaultdict, Counter, OrderedDict, deque
from functools import reduce
from operator import mul
maxx = float('inf')
I = lambda : int(sys.stdin.buffer.readline())
tup = lambda : map(int, sys.stdin.buffer.readline().split())
lint = lambda : [int(x) for x in sys.stdin.buffer.readline().split()]
S = lambda : sys.stdin.readline().strip('\n')
grid = lambda r: [lint() for i in range(r)]
stpr = lambda x: sys.stdout.write(f'{x}' + '\n')
star = lambda x: print(' '.join(map(str, x)))
localsys = 0
start_time = time.time()
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
for _ in range(I()):
	(n, k) = tup()
	(A, B, ans) = (sorted(map(lambda x: x - 1, lint()), reverse=True), lint(), 0)
	for i in range(n):
		if i < A[i]:
			ans += min(B[A[i]], B[i])
		else:
			ans += B[A[i]]
	print(ans)
if localsys:
	print('\n\nTime Elased :', time.time() - start_time, 'seconds')


def ans(arr, cost):
	arr.sort()
	total = j = 0
	for i in range(len(arr) - 1, -1, -1):
		if j < arr[i] - 1 and cost[j] < cost[arr[i] - 1]:
			total += cost[j]
			j += 1
		else:
			total += cost[arr[i] - 1]
	print(total)
t = int(input())
for i in range(t):
	x = input()
	arr1 = list(map(int, input().split(' ')))
	arr2 = list(map(int, input().split(' ')))
	ans(arr1, arr2)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	ans = 0
	a = 0
	k.sort()
	k.reverse()
	for i in k:
		if a == len(c) or c[a] > c[i - 1]:
			ans += c[i - 1]
		else:
			ans += c[a]
			a += 1
	print(ans)


import sys

def strangeBdayParty():
	for _ in range(int(input())):
		(n, m) = map(int, input().split())
		k = [int(i) for i in input().split()]
		c = [int(i) for i in input().split()]
		(ans, base) = (0, 0)
		k.sort(reverse=True)
		for i in k:
			if i - 1 > base:
				ans += c[base]
				base += 1
			else:
				ans += c[i - 1]
		print(ans)

def main():
	strangeBdayParty()
main()


for _ in range(int(input())):
	nm = list(map(int, input().split()))
	n = nm[0]
	m = nm[1]
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	cost = 0
	k.sort(reverse=True)
	for i in range(len(k)):
		k[i] -= 1
	for i in range(len(k)):
		cost += c[min(i, k[i])]
	print(cost)


import math
import sys

class Read:

	@staticmethod
	def string():
		return input()

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	(n, m) = Read.list_int()
	k = Read.list_int()
	c = Read.list_int()
	k.sort(reverse=True)
	res = 0
	current = 0
	for i in range(n):
		if current < k[i]:
			res += c[current]
			current += 1
		else:
			res += c[k[i] - 1]
	print(res)
query_count = Read.int()
while query_count:
	query_count -= 1
	solve()


t = int(input())
for j in range(t):
	(a, b) = list(map(int, input().split()))
	L = sorted(map(lambda x: x - 1, list(map(int, input().split()))))[::-1]
	M = list(map(int, input().split()))
	sum = 0
	for i in range(a):
		if i <= L[i]:
			x = i
		else:
			x = L[i]
		sum += M[x]
	print(sum)


import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort()
	k.reverse()
	ans = 0
	pointer = 0
	pointer2 = 0
	for i in range(n):
		if pointer <= k[i] - 1:
			ans += c[pointer]
			pointer += 1
		else:
			ans += c[k[i] - 1]
	print(ans)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
import time
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from collections import defaultdict as dd, deque as dq, Counter as dc
import math, string
start_time = time.time()

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())

def getMat(n):
	return [getInts() for _ in range(n)]

def isInt(s):
	return '0' <= s[0] <= '9'
MOD = 10 ** 9 + 7

def solve():
	(N, M) = getInts()
	K = getInts()
	C = getInts()
	best = 10 ** 18
	pref_cash = [0]
	curr = 0
	K.sort()
	for c in C:
		curr += c
		pref_cash.append(curr)
	pref_pres = [0]
	curr = 0
	for k in K:
		curr += C[k - 1]
		pref_pres.append(curr)
	for pres in range(N + 1):
		if N - pres >= len(pref_cash) or pres >= len(pref_pres):
			continue
		best = min(best, pref_pres[pres] + pref_cash[N - pres])
	return best
for _ in range(getInt()):
	print(solve())


def strange(n, m, k, c):
	c2 = c.copy()
	c2.reverse()
	k.sort()
	cost = 0
	for i in range(n - 1, -1, -1):
		if len(c2) != 0:
			if c[k[i] - 1] > c2[-1]:
				cost += c2[-1]
				c2.pop()
			else:
				cost += c[k[i] - 1]
		else:
			cost += c[k[i] - 1]
	return cost
for i in range(int(input())):
	[n, m] = list(map(int, input().split()))
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(strange(n, m, k, c))


import math
t = int(input())
while t:
	(n, m) = [int(i) for i in input().split()]
	k = input().split()
	c = input().split()
	for i in range(n):
		k[i] = int(k[i]) - 1
	for i in range(m):
		c[i] = int(c[i])
	k.sort()
	i = n - 1
	j = 0
	ans = 0
	while j <= k[i] and i >= 0 and (j < m):
		ans += c[j]
		i -= 1
		j += 1
	for x in range(i + 1):
		ans += c[k[x]]
	print(ans)
	t -= 1


t = int(input())
for Cases in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	k.sort(reverse=True)
	c = list(map(int, input().split()))
	rem = set()
	num = 0
	j = 0
	for i in k:
		if i - 1 in rem:
			num += c[i - 1]
			rem.add(i - 1)
		elif i - 1 not in rem:
			num += c[j]
			rem.add(j)
			j += 1
	print(num)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
(t,) = map(int, sys.stdin.readline().split())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	arrk = list(map(int, sys.stdin.readline().split()))
	arrc = list(map(int, input().split()))
	arrk.sort()
	ans = 0
	ak = collections.deque()
	for c in arrk:
		ak.append(c)
		if len(ak) > c:
			ans += arrc[ak.popleft() - 1]
	while ak and arrc[ak[0] - 1] < arrc[len(ak) - 1]:
		ans += arrc[ak.popleft() - 1]
	print(ans + sum(arrc[:len(ak)]))


t = int(input())
for i in range(t):
	nAndm = list(input().split(' '))
	n = int(nAndm[0])
	m = int(nAndm[1])
	nNumbers = list(input().split(' '))
	nNumbersInt = [int(k) for k in nNumbers]
	mgifts = list(input().split(' '))
	mgiftsInt = [int(j) for j in mgifts]
	nNumbersInt.sort(reverse=True)
	cost = 0
	for j in range(n):
		if nNumbersInt[j] > j:
			cost = cost + mgiftsInt[j]
		else:
			cost = cost + mgiftsInt[nNumbersInt[j] - 1]
	print(cost)


for t in range(int(input())):
	(n, m) = map(int, input().split())
	gue = tuple(map(int, input().split()))
	co = tuple(map(int, input().split()))
	gue = sorted(gue)
	gue.reverse()
	dp = [999999999999999999] * n
	j = 0
	for i in range(n):
		if j < m:
			dp[i] = min(dp[i], co[gue[i] - 1], co[j])
			if dp[i] == co[j]:
				j += 1
		if j >= m:
			dp[i] = co[gue[i] - 1]
	print(sum(dp))


tt = int(input())
for _ in range(tt):
	(n, m) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a = [i - 1 for i in a]
	a.sort(reverse=True)
	(x, ans) = (0, 0)
	for i in range(n):
		if a[i] >= x:
			ans += b[x]
			x += 1
		else:
			ans += b[a[i]]
	print(ans)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	k.sort(reverse=True)
	c = list(map(int, input().split()))
	idx = 0
	ans = 0
	for i in range(n):
		if idx < k[i] - 1:
			ans += c[idx]
			idx += 1
		else:
			ans += c[k[i] - 1]
	print(ans)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort(reverse=True)
	c.sort()
	s = set()
	j = 0
	sum = 0
	for i in range(n):
		if c[k[i] - 1] > c[j]:
			sum += c[j]
			s.add(c[j])
			j = j + 1
		elif c[k[i] - 1] <= c[j]:
			sum += c[k[i] - 1]
	print(sum)


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	list1 = list(map(int, input().split()))
	list2 = list(map(int, input().split()))
	list1.sort(reverse=True)
	num = 0
	for i in range(n):
		if list1[i] >= i + 1:
			num += list2[i]
		else:
			num += list2[list1[i] - 1]
	print(num)


t = int(input())
for _ in range(t):
	(n, m) = tuple(map(int, input().split()))
	klist = list(map(int, input().split()))
	jlist = list(map(int, input().split()))
	klist.sort(reverse=True)
	s = 0
	i = 0
	for k in klist:
		if jlist != []:
			if jlist[i] < jlist[k - 1]:
				s = s + jlist[i]
				i = i + 1
			else:
				s = s + jlist[k - 1]
		else:
			s = s + jlist[k - 1]
	print(s)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ar = list(map(int, input().split()))
	br = list(map(int, input().split()))
	ar.sort(reverse=True)
	ans = 0
	for i in range(n):
		if ar[i] - 1 > i:
			ans += br[i]
		else:
			ans += br[ar[i] - 1]
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, m) = list(map(int, stdin.readline().split()))
	k = sorted(list(map(int, stdin.readline().split())), reverse=True)
	c = list(map(int, stdin.readline().split()))
	res = 0
	j = 0
	for i in k:
		if c[i - 1] > c[j]:
			res += c[j]
			j += 1
		else:
			res += c[i - 1]
	print(res)


for i in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort()
	k = k[::-1]
	present = 0
	ans = 0
	for j in range(n):
		if present < m and c[k[j] - 1] > c[present]:
			ans += c[present]
			present += 1
		else:
			ans += c[k[j] - 1]
	print(ans)


casos = int(input())
for i in range(casos):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	k = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	total = 0
	l = 1
	k = reversed(sorted(k))
	c.insert(0, 666)
	for ki in k:
		if c[ki] > c[l] and l < ki:
			total = total + c[l]
			l = l + 1
		else:
			total = total + c[ki]
	print(total)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):

	def __init__(self, file):
		self.newlines = 0
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline()

def RL():
	return map(int, sys.stdin.readline().split())

def RLL():
	return list(map(int, sys.stdin.readline().split()))

def N():
	return int(input())

def S():
	return input().strip()

def print_list(l):
	print(' '.join(map(str, l)))
for _ in range(N()):
	(n, m) = RL()
	a = [0] + sorted(RLL(), reverse=True)
	b = [0] + RLL()
	res = 0
	now = 1
	for k in a:
		if now < k:
			res += b[now]
			now += 1
		else:
			res += b[k]
	print(res)


def solver(inputs, friends, gifts):
	cost = 0
	gift_index = 0
	friends.sort(reverse=True)
	for y in range(inputs[0]):
		if friends[y] > gift_index:
			cost = cost + gifts[gift_index]
			gift_index = gift_index + 1
		else:
			cost = cost + gifts[friends[y] - 1]
	return cost
n = int(input())
for x in range(n):
	inputs = input().split(' ')
	inputs = [int(i) for i in inputs]
	friends = input().split(' ')
	friends = [int(i) for i in friends]
	gifts = input().split(' ')
	gifts = [int(i) for i in gifts]
	print(solver(inputs, friends, gifts))


import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
	(n, m) = map(int, input().split())
	K = sorted(map(int, input().split()), reverse=True)
	C = list(map(int, input().split()))
	ind = 0
	ANS = 0
	for k in K:
		k -= 1
		if ind < k:
			ANS += C[ind]
			ind += 1
		else:
			ANS += C[k]
	print(ANS)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted(list(map(int, input().split())), reverse=True)
	c = list(map(int, input().split()))
	s = 0
	ans = 0
	for x in k:
		if c[s] < c[x - 1]:
			ans += c[s]
			s += 1
		else:
			ans += c[x - 1]
	print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted([int(i) for i in input().split()])
	c = [''] + [int(i) for i in input().split()]
	(i, res) = (1, 0)
	for ind in k[::-1]:
		if ind >= i:
			res += c[i]
			i += 1
		else:
			res += c[ind]
	print(res)


def solve():
	k = []
	c = []
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	k = list(map(int, list(input().split())))
	c = list(map(int, list(input().split())))
	k.sort(reverse=True)
	i = 0
	sum = 0
	for element in k:
		if c[element - 1] > c[i]:
			sum += c[i]
			i += 1
		else:
			sum += c[element - 1]
	print(sum)
q = int(input())
for i in range(q):
	solve()


from collections import Counter
t = int(input())
while t > 0:
	t -= 1
	(n, m) = (int(i) for i in input().split())
	p = [int(i) for i in input().split()]
	k = [int(i) for i in input().split()]
	c = ans = 0
	for i in sorted(p, reverse=True):
		if c < i:
			ans += k[c]
			c += 1
		else:
			ans += k[i - 1]
	print(ans)


from sys import stdin
input = stdin.readline
for i in range(int(input())):
	(n, m) = map(int, input().split())
	t = sorted(list(map(int, input().split())), reverse=True)
	t1 = list(map(int, input().split()))
	b = []
	for i in t:
		b.append(t1[i - 1])
	for i in range(n):
		if t1[i] < b[i]:
			b[i] = t1[i]
		else:
			break
		if i == m:
			break
	print(sum(b))


t = int(input())

def solve():
	(n, m) = list(map(int, input().split()))
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort()
	used = [0] * n
	count = n - 1
	i = 0
	while i < m or count != -1:
		used[count] = min(c[i], c[k[count] - 1])
		if i < m - 1:
			i += 1
		if count > 0:
			count -= 1
		else:
			break
	print(sum(used))
for j in range(t):
	solve()


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	k = [int(i) for i in input().split()]
	c = [int(i) for i in input().split()]
	k.sort(reverse=True)
	ans = 0
	a1 = 0
	for i in range(n):
		if a1 < k[i]:
			ans += c[a1]
			a1 += 1
		else:
			ans += c[k[i] - 1]
	print(ans)


t = int(input())
for j in range(t):
	(n, m) = map(int, input().split())
	k = [i for i in map(int, input().split())]
	c = [i for i in map(int, input().split())]
	k.sort(reverse=True)
	rez = 0
	nom = 0
	for i in range(n):
		if nom < m and k[i] > nom:
			rez = rez + c[nom]
			nom = nom + 1
		else:
			rez = rez + c[k[i] - 1]
	print(rez)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	ks = [int(x) - 1 for x in input().split()]
	cs = [int(x) for x in input().split()]
	ks.sort(reverse=True)
	t = 0
	for (i, k) in enumerate(ks):
		t += cs[min(i, k)]
	print(t)


for test in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = [0] + list(map(int, input().split()))
	k.sort(reverse=True)
	ans = 0
	i = 0
	j = 1
	while i < len(k) and c[k[i]] > c[j]:
		ans += c[j]
		i += 1
		j += 1
	while i < len(k):
		ans += c[k[i]]
		i += 1
	print(ans)


def solve(k, c):
	k = sorted(k)[::-1]
	n = len(k)
	m = len(c)
	ans = 0
	for i in range(n):
		if i < m:
			ans += min(c[i], c[k[i] - 1])
		else:
			ans += c[k[i] - 1]
	return ans
t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(solve(k, c))


import math
for t in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a.sort()
	i = 0
	ans = 0
	for j in range(n - 1, -1, -1):
		if b[i] >= b[a[j] - 1]:
			ans += b[a[j] - 1]
		else:
			ans += b[i]
			i += 1
	print(ans)


t = int(input())
while t:
	t = t - 1
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	dp = [0] * n
	for i in range(n):
		dp[i] = b[a[i] - 1]
	dp.sort()
	dp.reverse()
	ptr = 0
	for i in range(n):
		if dp[i] > b[ptr]:
			dp[i] = b[ptr]
			ptr += 1
	sum = 0
	for i in range(n):
		sum += dp[i]
	print(sum)


def solve(n, m, guests, presents):
	guests.sort(reverse=True)
	guestsIndex = 0
	presentsIndex = 0
	ans = 0
	while guestsIndex < n and presentsIndex < m:
		if presents[presentsIndex] < presents[guests[guestsIndex] - 1]:
			ans += presents[presentsIndex]
			presentsIndex += 1
		else:
			ans += presents[guests[guestsIndex] - 1]
		guestsIndex += 1
	while guestsIndex < n:
		ans += presents[guests[guestsIndex] - 1]
	return ans
for x in range(int(input())):
	(n, m) = list(map(int, input().split()))
	guests = list(map(int, input().split()))
	presents = list(map(int, input().split()))
	print(solve(n, m, guests, presents))


import re
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	k_list = list(map(int, input().split()))
	c_list = list(map(int, input().split()))
	k_list.sort(reverse=True)
	pointer = 1
	result = 0
	for k in k_list:
		if pointer > k:
			result += c_list[k - 1]
			continue
		if pointer < m and c_list[pointer - 1] < c_list[k - 1]:
			result += c_list[pointer - 1]
			pointer += 1
			continue
		result += c_list[k - 1]
	print(result)


def sbp(friends, gifts):
	sum = 0
	friends.sort(reverse=True)
	f = 0
	while f < len(friends):
		index = f if f < friends[f] - 1 else friends[f] - 1
		sum += gifts[index]
		f += 1
	print(sum, end=' ')
t = int(input())
for i in range(t):
	(n, m) = input().split(' ')
	friends = input().split(' ')
	integer_friends = map(int, friends)
	integer_f = list(integer_friends)
	gifts = input().split(' ')
	integer_gifts = map(int, gifts)
	integer_g = list(integer_gifts)
	sbp(integer_f, integer_g)
	if i < t - 1:
		print()


t = int(input())
for q in range(t):
	n = [int(x) for x in input().split()]
	(len_k, len_c) = (n[0], n[1])
	k = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	k = sorted(k)
	s = 0
	for i in k:
		s += c[i - 1]
	g = 0
	min_score = s
	flag = 1
	gift_val = len_c
	for i in range(len_c):
		if i > len_k - 1:
			break
		if gift_val == 0:
			break
		else:
			gift_val = min(k[-i - 1] - 1, gift_val - 1)
		s -= c[k[-i - 1] - 1]
		g += c[i]
		score = s + g
		if min_score > score:
			min_score = score
	print(min_score)


import math
from collections import Counter

def strange_birthday_party(friends, presents):
	count = 0
	cost = 0
	friend_counter = Counter(friends)
	use_present = True
	buy_present = 0
	for index in sorted(friend_counter.keys(), reverse=True):
		value = friend_counter[index]
		if use_present and index > count:
			count += value
			buy_present = count
			if count >= index:
				use_present = False
				cost += presents[index - 1] * (count - index)
				buy_present = index
		else:
			cost += presents[index - 1] * value
	return cost + sum(presents[:buy_present])
num_test = int(input())
for i in range(num_test):
	(n, m) = [int(i) for i in input().split()]
	friends = [int(i) for i in input().split()]
	presents = [int(i) for i in input().split()]
	print(strange_birthday_party(friends, presents))


task_number = int(input())
for _ in range(task_number):
	(n, m) = map(int, input().split())
	K = [int(x) - 1 for x in input().split()]
	C = [int(x) for x in input().split()]
	result = 0
	p_index = 0
	K.sort(reverse=True)
	for k in K:
		if p_index < k:
			result += C[p_index]
			p_index += 1
		else:
			result += C[k]
	print(result)


for l in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	rev_sorted_k = k
	rev_sorted_k.sort(reverse=True)
	c = list(map(int, input().split()))
	c_copy = c
	cost = 0
	r = 0
	for i in rev_sorted_k:
		a = c[i - 1]
		if r < m:
			b = c_copy[r]
			if a >= b:
				cost += b
				r += 1
			else:
				cost += a
		else:
			cost += c[i - 1]
	print(cost)


t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	l1 = [int(i) for i in input().split()]
	l2 = [int(i) for i in input().split()]
	l1.sort(reverse=True)
	ans = 0
	for i in range(n):
		if i + 1 < l1[i]:
			ans = ans + l2[i]
		else:
			ans = ans + l2[l1[i] - 1]
	print(ans)


T = int(input())
for _ in range(T):
	(M, N) = map(int, input().split())
	nums = list(map(int, input().split()))
	P = list(map(int, input().split()))
	nums.sort(reverse=True)
	ans = 0
	idx = 0
	for i in range(len(nums)):
		if idx >= len(P) or P[nums[i] - 1] < P[idx]:
			ans += P[nums[i] - 1]
		else:
			ans += P[idx]
			idx += 1
	print(ans)


import sys
input = sys.stdin.readline
T = int(input())
for t in range(T):
	(n, m) = map(int, input().split())
	inds_cost = list(map(int, input().split()))
	costs = list(map(int, input().split()))
	up_to_costs = [costs[pos - 1] for pos in inds_cost]
	sol = 0
	start = 10 ** 10
	up_to_costs.sort(reverse=True)
	for i in range(min(m, n)):
		if up_to_costs[i] > costs[i]:
			sol += costs[i]
		else:
			start = i
			break
	sol += sum(up_to_costs[start:])
	print(sol)


from sys import stdin, stdout
input = stdin.readline
from collections import defaultdict as dd
import math

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

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

def getis():
	return map(str, input().strip().split())

def getls():
	return list(map(str, input().strip().split()))

def gets():
	return input().strip()

def geta():
	return int(input())

def print_s(s):
	stdout.write(s + '\n')

def solve():
	for _ in range(geta()):
		(n, m) = geti()
		a = getl()
		cost = getl()
		a.sort(reverse=True)
		j = 0
		ans = 0
		for i in range(n):
			if a[i] >= j + 1 and j < n:
				ans += cost[j]
				j += 1
			else:
				ans += cost[a[i] - 1]
		print(ans)
solve()


t = int(input())

def f(n, k, m, c):
	k.sort()
	k = k[::-1]
	x = 0
	summ = 0
	for i in range(n):
		if c[x] < c[k[i] - 1]:
			summ += c[x]
			x += 1
			srop = 0
		else:
			summ += c[k[i] - 1]
	return summ
for i in range(t):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(f(n, k, m, c))


list_ans = []
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	list1 = list(map(int, input().split()))
	list2 = list(map(int, input().split()))
	list1.sort(reverse=True)
	ans = 0
	a = 0
	for x in list1:
		if x > a:
			ans += list2[a]
			a += 1
		else:
			ans += list2[x - 1]
	list_ans.append(ans)
print(*list_ans, sep='\n')


for k in range(int(input())):
	(n, m) = list(map(int, input().split()))
	kls = list(map(int, input().split()))
	pls = list(map(int, input().split()))
	kls = sorted(kls, reverse=True)
	ans = 0
	j = 0
	for i in range(n):
		num = kls[i] - 1
		money = pls[num]
		if j < m:
			gift = pls[j]
		else:
			gift = 1e+99
		if money <= gift:
			ans += money
		else:
			ans += gift
			j += 1
	print(ans)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = [*map(int, input().split())]
	c = [*map(int, input().split())]
	c_cons = [False] * len(c)
	k.sort(reverse=True)
	next_g_ind = 0
	biggest_g_ind = k[0]
	total_cost = 0
	for x in k:
		if next_g_ind <= x - 1 and (not c_cons[next_g_ind]):
			c_cons[next_g_ind] = True
			total_cost += c[next_g_ind]
			next_g_ind += 1
		else:
			total_cost += c[x - 1]
	print(total_cost)


for test_num in range(int(input())):
	(f, m) = input().split()
	f_list = list(map(int, input().split()))
	m_list = list(map(int, input().split()))
	f_list.sort()
	f_list.reverse()
	money = 0
	present = 0
	for i in f_list:
		if present < i:
			money += m_list[present]
			present += 1
		else:
			money += m_list[i - 1]
	print(money)


t = int(input())
for _ in range(t):
	(n, x) = list(map(int, input().split()))
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	k.sort(reverse=True)
	k = [i - 1 for i in k]
	ans = 0
	j = 0
	for i in range(len(k)):
		if k[i] >= j:
			ans += c[j]
			j += 1
		else:
			ans += c[k[i]]
	print(ans)


from sys import stdin
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
for _ in range(it()):
	(n, m) = mp()
	c = sorted(mp(), reverse=True)
	k = mp()
	(i, t) = (0, 0)
	ans = 0
	while i < n and t < m:
		if k[c[i] - 1] <= k[t]:
			ans += k[c[i] - 1]
			i += 1
		else:
			ans += k[t]
			t += 1
			i += 1
		if t >= m:
			j = i
			while j <= n:
				ans += k[c[j] - 1]
				j += 1
			break
	print(ans)


import sys
t = int(input())
for i in range(t):
	(n, m) = list(map(int, sys.stdin.readline().strip().split()))
	F = list(map(int, sys.stdin.readline().strip().split()))
	P = list(map(int, sys.stdin.readline().strip().split()))
	F.sort()
	cost = 0
	c_ptr = 0
	for k in range(n - 1, -1, -1):
		if c_ptr > m - 1 or P[c_ptr] > P[F[k] - 1]:
			cost += P[F[k] - 1]
		else:
			cost += P[c_ptr]
			c_ptr += 1
	print(cost)


def strangeBDParty(n: int, m: int, k: [int], c: [int]) -> int:
	k.sort(reverse=True)
	cost = l = 0
	for i in range(n):
		if l < m and c[l] < c[k[i] - 1]:
			cost += c[l]
			l += 1
		else:
			cost += c[k[i] - 1]
	return cost
rep = int(input())
for _ in range(rep):
	(n, m) = list(map(int, input().split()))
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	print(strangeBDParty(n, m, k, c))


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	k = list(sorted(map(int, input().split()), reverse=True))
	c = list(map(int, input().split()))
	friend_costs = list(map(lambda idx: c[idx - 1], k))
	npresent = 0
	sum = 0
	for i in range(n):
		if npresent < len(c) and npresent <= k[i] and (c[npresent] < friend_costs[i]):
			sum += c[npresent]
			npresent += 1
		else:
			sum += friend_costs[i]
	print(sum)


import sys
input = sys.stdin.buffer.readline

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))
allAns = []
t = int(input())
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	k = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	k.sort(reverse=True)
	totalCost = 0
	cJ = 0
	for ki in k:
		ki -= 1
		if cJ < ki:
			totalCost += c[cJ]
			cJ += 1
		else:
			totalCost += c[ki]
	allAns.append(totalCost)
multiLineArrayPrint(allAns)


from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
import heapq
import sys

def input():
	return sys.stdin.readline().rstrip()

def sol():
	(n, m) = map(int, input().split())
	idxa = list(map(int, input().split()))
	cost = list(map(int, input().split()))
	idxa.sort()
	lpos = 0
	ans = 0
	for i in range(n - 1, -1, -1):
		idx = idxa[i] - 1
		if lpos <= idx:
			ans += cost[lpos]
			lpos += 1
		else:
			ans += cost[idx]
	print(ans)
	return

def main():
	testcase = int(input())
	for i in range(testcase):
		sol()
main()


T = int(input())
for _ in range(T):
	(num_friends, num_presents) = map(int, input().split(' '))
	ks = sorted([int(k) - 1 for k in input().split(' ')])
	assert len(ks) == num_friends
	prices = list(map(int, input().split(' ')))
	assert len(prices) == num_presents
	total_cost = 0
	num_taken = 0
	for k in reversed(ks):
		direct_cost = prices[k]
		present_cost = prices[num_taken]
		if present_cost >= direct_cost:
			total_cost += direct_cost
		else:
			total_cost += present_cost
			num_taken += 1
	print(total_cost)


import sys
import math
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	c = list(map(int, input().split()))
	a.sort(reverse=True)
	j = 0
	ans = 0
	for i in range(n):
		if a[i] - 1 <= j:
			ans += c[a[i] - 1]
		else:
			ans += c[j]
			j += 1
	print(ans)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(N, M) = map(int, input().split())
	a = list(map(int, input().split()))
	c = list(map(int, input().split()))
	a.sort()
	i = 0
	res = 0
	while len(a):
		x = a.pop()
		if x > i:
			res += c[i]
			i += 1
		else:
			a.append(x)
			break
	for x in a:
		res += c[x - 1]
	print(res)


import sys
input = lambda : sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
	(N, M) = map(int, input().split())
	A = sorted([int(a) - 1 for a in input().split()])
	B = [int(a) for a in input().split()]
	ans = 0
	i = 0
	while A:
		a = A.pop()
		if i < a:
			ans += B[i]
			i += 1
		else:
			ans += B[a]
	print(ans)


def solve():
	t = inp()
	for _ in range(t):
		(n, k) = mul()
		l = seq()
		m = seq()
		l.sort(reverse=True)
		pos = 0
		total = 0
		for j in range(n):
			if l[j] - 1 > pos:
				total += m[pos]
				pos += 1
			else:
				total += m[l[j] - 1]
		print(total)
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))
solve()


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	gifts = sorted(list(map(int, input().split())), reverse=True)
	cost = list(map(int, input().split()))
	res = 0
	place = 0
	for index in range(n):
		if gifts[index] - 1 < place:
			res += cost[gifts[index] - 1]
		elif gifts[index] - 1 >= place:
			res += cost[place]
			place += 1
	print(res)


test = int(input())
while test:
	(n, o) = map(int, input().split())
	li = list(map(int, input().split()))
	li1 = list(map(int, input().split()))
	li.sort(reverse=True)
	(c, ans) = (0, 0)
	for i in li:
		if li1[c] < li1[i - 1]:
			ans += li1[c]
			c += 1
		else:
			ans += li1[i - 1]
	print(ans)
	test -= 1


t = int(input())
outputs = []
for _ in range(t):
	(n, m) = map(int, input().split())
	ks = list(map(int, input().split()))
	cs = list(map(int, input().split()))
	csbool = [True] * m
	ks.sort(reverse=True)
	cena = 0
	w = 0
	for i in range(n):
		if ks[i] - 1 > w:
			cena += cs[w]
			w += 1
		else:
			cena += cs[ks[i] - 1]
	outputs.append(cena)
for odp in outputs:
	print(odp)


from math import ceil
t = int(input())
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	v = []
	for i in k:
		v.append(c[i - 1])
	v.sort(reverse=True)
	for i in range(min(n, m)):
		if v[i] > c[i]:
			v[i] = c[i]
	print(sum(v))


gans = []
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = list(map(int, input().split()))
	c = list(map(int, input().split()))
	p = [0] * n
	k.sort()
	for i in range(n):
		p[i] = c[k[i] - 1]
	for i in range(1, n):
		p[i] += p[i - 1]
	for i in range(1, m):
		c[i] += c[i - 1]
	ans = p[-1]
	if m >= n:
		ans = min(ans, c[n - 1])
	for x in range(max(0, n - m - 1), n - 1):
		cur = p[x] + c[n - x - 2]
		ans = min(ans, cur)
	gans.append(ans)
print('\n'.join(map(str, gans)))


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None] * T
for qu in range(T):
	(N, M) = map(int, readline().split())
	K = list(map(lambda x: int(x) - 1, readline().split()))
	K.sort(reverse=True)
	C = list(map(int, readline().split()))
	L = list(range(N))
	L.sort(key=lambda x: K[x], reverse=True)
	ans = 0
	nxt = 0
	for k in K:
		if nxt <= k:
			ans += C[nxt]
			nxt += 1
		else:
			ans += C[k]
	Ans[qu] = ans
print('\n'.join(map(str, Ans)))


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	a.sort()
	(j, res) = (0, 0)
	for _ in range(n - 1, -1, -1):
		if j <= a[_] - 1:
			res += b[j]
			j += 1
		else:
			res += b[a[_] - 1]
	print(res)


for testcase in range(int(input())):
	(n, m) = map(int, input().split(' '))
	k = sorted(list(map(lambda x: int(x) - 1, input().split(' '))))[::-1]
	c = list(map(int, input().split(' ')))
	suma = 0
	for i in range(len(k)):
		if i < k[i]:
			suma += c[i]
		else:
			suma += c[k[i]]
	print(suma)


t = int(input())
res = []
for i in range(t):
	(n, m) = list(map(int, input().split()))
	k = sorted(list(map(int, input().split())), reverse=True)
	c = list(map(int, input().split()))
	s = 0
	for i in range(n):
		s += c[i] if i < k[i] else c[k[i] - 1]
	res.append(s)
print(*res, sep='\n')


from math import inf

def main():
	t = int(input())
	for _ in range(t):
		(n, m) = map(int, input().split())
		ks = [int(x) for x in input().split()]
		cs = [0] + [int(x) for x in input().split()]
		ks.sort(reverse=True)
		min_sum = 0
		cur_present = 1
		for (i, k) in enumerate(ks):
			den = cs[k]
			pod = cs[cur_present] if cur_present <= m else inf
			if den > pod:
				cur_present += 1
				min_sum += pod
			else:
				min_sum += den
		print(min_sum)
main()


a = int(input())
sum2 = []
for i in range(a):
	list1 = input().split(' ')
	n = int(list1[0])
	m = int(list1[1])
	list2 = input().split(' ')
	list2 = list(map(int, list2))
	list3 = input().split(' ')
	list3 = list(map(int, list3))
	list2.sort(reverse=True)
	sum = 0
	x = 1
	for k in list2:
		if k > x:
			sum = sum + list3[x - 1]
		else:
			sum = sum + list3[k - 1]
		x = x + 1
	sum2.append(sum)
	list1.clear()
	list2.clear()
	list3.clear()
for sum3 in sum2:
	print(sum3)


import sys
input = sys.stdin.readline
getint = lambda : int(input())
getints = lambda : [int(a) for a in input().split()]

def solve():
	(n, _) = getints()
	friends = getints()
	prices = getints()
	friends = sorted(friends, reverse=True)
	i = 0
	sum = 0
	while i < n and friends[i] - 1 > i:
		sum += prices[i]
		i += 1
	while i < n:
		sum += prices[friends[i] - 1]
		i += 1
	print(sum)
t = getint()
for i in range(t):
	solve()


t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	v = [int(x) for x in input().split()]
	a = [int(x) for x in input().split()]
	v.sort()
	v.reverse()
	j = 0
	cnt = 0
	for i in range(n):
		if j < m and j < v[i]:
			cnt = cnt + a[j]
			j += 1
		else:
			cnt = cnt + a[v[i] - 1]
	print(cnt)


import math, sys
from collections import defaultdict, Counter
input = sys.stdin.readline
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	k = sorted(map(int, input().split()))
	c = list(map(int, input().split()))
	ans = 0
	i = 0
	while k:
		node = k.pop()
		if c[i] < c[node - 1]:
			ans += c[i]
			i += 1
		else:
			ans += c[node - 1]
	print(ans)


def main():
	for _ in range(int(input())):
		(n, m) = list(map(int, input().split()))
		k = list(map(int, input().split()))
		c = list(map(int, input().split()))
		k.sort()
		cost = 0
		j = 0
		for i in range(n):
			if k[i] + i <= n:
				cost += c[k[i] - 1]
			else:
				cost += c[j]
				j += 1
		print(cost)
main()