Little Elephant likes lemonade.

When Little Elephant visits any room, he finds the bottle of the lemonade in that room that contains the greatest number of litres of lemonade and drinks it all.

There are n rooms (numbered from 0 to n-1), each contains C_{i} bottles. Each bottle has a volume (in litres). The first room visited by Little Elephant was P_{0}-th, the second - P_{1}-th, ..., the m-th - P_{m-1}-th room. Note that Little Elephant may visit a room more than once.

Find for Little Elephant the total volume of lemonade he has drunk.

------ Input ------ 

First line of the input contains single integer T - the number of test cases. T test cases follow. First line of each test case contains pair of integers n and m. Second line contains m integers separated by a single space - array P. Next n lines describe bottles in each room in such format: C_{i} V_{0} V_{1} ... VC_{i}-1, where V is the list of volumes (in liters) of all bottles in i-th room.

------ Output ------ 

In T lines print T integers - the answers for the corresponding test cases.

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

1 ≤ T ≤ 10

1 ≤ n, C_{i} ≤ 100

1 ≤ m ≤ 10^{4}

0 ≤ P_{i} < n

1 ≤ V_{i} ≤ 10^{5}

----- Sample Input 1 ------ 
2
3 3
0 2 1
3 1 2 3
1 7
2 4 7
4 7
0 1 3 0 1 0 0
1 7
3 9 4 5
7 1 2 3 4 5 6 7
1 1
----- Sample Output 1 ------ 
17
22

Difficulty: MEDIUM

Solutions:
tes = int(input())
for i in range(tes):
	(nr, mv) = map(int, input().split())
	visits = list(map(int, input().split()))
	(arr, drink) = ([], 0)
	for j in range(nr):
		bottles = list(map(int, input().split()))
		arr.append(bottles[0:1] + sorted(bottles[1:]))
	while True:
		for j in visits:
			if len(arr[j]) > 1:
				drink += arr[j][len(arr[j]) - 1]
				arr[j].pop(len(arr[j]) - 1)
		break
	print(drink)


from collections import Counter

def codechef(n, m, p, drink):
	k = Counter(p)
	ans = 0
	for (i, j) in k.items():
		drink[i].sort(reverse=True)
		ans += sum(drink[i][:j])
	return ans
t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	drink = []
	for i in range(n):
		temp = list(map(int, input().split()))
		drink.append(temp[1:])
	print(codechef(n, m, p, drink))


def main():
	try:
		for test in range(int(input())):
			(n, m) = map(int, input().split())
			P = list(map(int, input().split()))
			bottles: dict[int, list] = {}
			for index in range(n):
				x = list(map(int, input().split()))
				x.pop(0)
				bottles[index] = x
			sol = resolve(n, m, P, bottles)
			print(sol)
	except EOFError:
		return

def resolve(n: int, m: int, P: int, bottles: dict):
	count: int = 0
	for index in range(len(P)):
		room = P[index]
		if len(bottles[room]) > 0:
			maxValue = max(bottles[room])
			bottles[room].remove(maxValue)
			count += maxValue
	return count
main()


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	m = []
	for j in range(n):
		x = list(map(int, input().split()))
		x.remove(x[0])
		x.sort()
		m.append(x)
	total = 0
	for j in l:
		x = m[j]
		if len(x) > 0:
			y = x[len(x) - 1]
			m[j].pop()
			total += y
	print(total)


for _ in range(int(input())):
	(N, M) = map(int, input().split())
	ans = 0
	P = list(map(int, input().split()))
	volumes = []
	for i in range(N):
		a = list(map(int, input().split()))
		a = a[1:]
		a.sort()
		a = a[-1::-1]
		volumes.append(a)
	for i in range(M):
		a = P[i]
		if len(volumes[a]) > 0:
			ans += volumes[a][0]
			volumes[a].pop(0)
	print(ans)


for i in range(int(input())):
	(a, b) = map(int, input().split())
	li = list(map(int, input().split()))
	ls = []
	for j in range(a):
		ls.append(list(map(int, input().split())))
		ls[j].pop(0)
	su = 0
	for i in li:
		try:
			va = max(ls[i])
			su += va
			ls[i].remove(va)
		except:
			su += 0
	print(su)


tests = int(input())
for i in range(tests):
	(n, m) = map(int, input().split())
	rooms = list(map(int, input().split()))
	freq = {}
	for j in rooms:
		if j in freq:
			freq[j] += 1
		else:
			freq[j] = 1
	ans = 0
	for j in range(n):
		string = input()
		if j in freq:
			arr = list(map(int, string.split()))
			drinks = sorted(arr[1:])
			ans += sum(drinks[-1 * freq[j]:])
	print(ans)


T = int(input())
for j in range(T):
	(N, M) = map(int, input().split())
	s = list(map(int, input().split()))
	t = []
	for i in range(N):
		b = list(map(int, input().split()))
		t.append(b[1:])
	for i in t:
		i.sort()
	c = 0
	for i in s:
		c += t[i][-1]
		t[i][-1] = 0
		t[i].sort()
	print(c)


def Solve(p, c):
	answer = 0
	for i in p:
		if len(c[i]) == 0:
			answer = answer + 0
		else:
			answer = answer + max(c[i])
			c[i].remove(max(c[i]))
	return answer
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	c = []
	for i in range(n):
		c.append(list(map(int, input().split()))[1:])
	print(Solve(p, c))


T = int(input())
for i in range(T):
	(n, m) = map(int, input().split())
	room = list(map(int, input().split()))
	total = 0
	for i in range(n):
		a = list(map(int, input().split()))
		a.remove(a[0])
		a.sort()
		a.reverse()
		x = room.count(i)
		if x >= len(a):
			total += sum(a)
		else:
			for j in range(x):
				total += a[j]
	print(total)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	l = list()
	c = 0
	for _ in range(n):
		x = list(map(int, input().split()))
		x.pop(0)
		l.append(x)
	for i in range(m):
		if len(l[p[i]]) != 0:
			y = max(l[p[i]])
			l[p[i]].remove(y)
			c = c + y
		else:
			pass
	print(c)


for u in range(int(input())):
	(n, k) = map(int, input().split())
	d = list(map(int, input().split()))
	l = []
	c = [0] * n
	s = 0
	for i in d:
		c[i] += 1
	for i in range(n):
		f = list(map(int, input().split()))
		x = f[0]
		f = f[1:]
		f.sort(reverse=True)
		l.append([x] + f)
	for i in range(n):
		if c[i] > 0:
			if c[i] <= l[i][0]:
				s += sum(l[i][1:c[i] + 1])
			else:
				s += sum(l[i][1:])
	print(s)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	li = []
	c = 0
	for i in range(n):
		a = list(map(int, input().split()))
		a = a[1:]
		a.sort()
		li.append(a)
	for i in range(m):
		e = li[p[i]]
		if e != []:
			c += e[-1]
			e.pop()
	print(c)


t = int(input())
for t1 in range(t):
	(n, m) = map(int, input().split())
	order = list(map(int, input().split()))
	rooms = []
	for z in range(n):
		temp = list(map(int, input().split()))
		rooms.append(temp[1:])
	ans = 0
	for z in order:
		if len(rooms[z]) > 0:
			m = max(rooms[z])
			rooms[z].remove(m)
			ans += m
	print(ans)


t = int(input())
while t > 0:
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	l = []
	for i in range(n):
		l.append(list(map(int, input().split()))[1:])
	c = 0
	for i in p:
		if l[i] != []:
			x = max(l[i])
			l[i].remove(x)
			c += x
	print(c)
	t -= 1


import collections
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	d = collections.Counter(p)
	total = 0
	for i in range(n):
		c = list(map(int, input().split()))
		c.pop(0)
		c.sort()
		if i in d:
			if d[i] >= len(c):
				total += sum(c)
			else:
				total += sum(c[-d[i]:])
	print(total)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = []
	ans = 0
	P = list(map(int, input().split()))
	for _ in range(n):
		i1 = list(map(int, input().split()))
		i1.pop(0)
		l.append(i1)
	for val in P:
		if len(l[val]) > 0:
			temp = max(l[val])
			ans += temp
			l[val].remove(temp)
	print(ans)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	mp = {}
	for j in range(n):
		(leng, *lis) = map(int, input().split())
		mp[j] = lis
	sumi = 0
	for j in p:
		z = mp[j]
		if len(z) == 0:
			continue
		else:
			maxi = max(z)
			sumi += maxi
			z.remove(maxi)
	print(sumi)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	rmvst = list(map(int, input().split()))
	cnt = []
	litres = []
	visited = [0] * n
	for j in range(n):
		l = list(map(int, input().split()))
		cnt.append(l[0])
		l = l[1:]
		l.sort()
		litres.append(l)
	tot = 0
	for j in rmvst:
		visited[j] += 1
		if visited[j] <= cnt[j]:
			tot = tot + litres[j][-visited[j]]
	print(tot)


T = int(input())
for i in range(T):
	(n, m) = map(int, input().split(' '))
	v = 0
	k = list(map(int, input().split(' ')))
	V = []
	for j in range(n):
		a = list(map(int, input().split(' ')))
		a.pop(0)
		a.sort()
		V.append(a)
	for j in range(m):
		a = k[j]
		if len(V[a]):
			v += V[a].pop()
	print(v)


for _ in range(int(input())):
	a = list()
	(x, y) = map(int, input().split())
	c = list(map(int, input().split()))
	for i in range(x):
		b = list(map(int, input().split()))
		del b[0]
		a.append(b)
	k = 0
	for i in c:
		if len(a[i]) != 0:
			p = max(a[i])
			k = k + p
			a[i].remove(p)
	print(k)


class Solution(object):

	def __init__(self):
		for _ in range(int(input())):
			self.get_data()
			self.solve()
			self.print_ans()
	pass

	def get_data(self):
		(self.n, self.m) = map(int, input().split())
		self.p = list(map(int, input().split()))
		self.a = []
		for _ in range(self.n):
			a = list(map(int, input().split()))
			a.pop(0)
			a.sort(reverse=True)
			self.a.append(a)
		pass

	def solve(self):
		self.ans = 0
		for x in self.p:
			if len(self.a[x]):
				self.ans += self.a[x][0]
				self.a[x].pop(0)
		pass

	def print_ans(self):
		print(self.ans)
		pass

def main():
	Solution()
	pass
main()


from collections import defaultdict
for _ in range(int(input())):
	(rooms, done) = [int(x) for x in input().split()]
	visits = [int(x) for x in input().split()]
	bottles = []
	for i in range(rooms):
		(indx, *volume) = [int(x) for x in input().split()]
		bottles.append(volume)
	drunk = 0
	for i in visits:
		curr = bottles[i]
		if len(curr) > 0:
			indx = curr.index(max(curr))
			drunk += curr[indx]
			del bottles[i][indx]
	print(drunk)


for j in range(int(input())):
	(n, m) = map(int, input().split())
	x = list(map(int, input().split()))
	y = []
	for i in range(n):
		z = list(map(int, input().split()))
		y.append(z[1:])
	for i in y:
		i.sort()
	ravan = 0
	for i in x:
		ravan += y[i][-1]
		y[i][-1] = 0
		y[i].sort()
	print(ravan)


for test_case in range(int(input())):
	(n, m) = map(int, input().split(' '))
	p = list(map(int, input().split(' ')))
	rooms = []
	for item in range(n):
		rooms.append(list(map(int, input().split(' ')))[1:])
	for room in rooms:
		room.sort()
	total = 0
	for value in p:
		total += rooms[value][-1]
		rooms[value][-1] = 0
		rooms[value].sort()
	print(total)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	P = list(map(int, input().split()))
	D = {}
	for i in range(n):
		C = list(map(int, input().split()))
		D[i] = C[1:]
	sum = 0
	for i in range(m):
		d = D[P[i]]
		maxm = -1
		ind = -1
		for j in range(len(d)):
			if maxm < d[j]:
				maxm = d[j]
				ind = j
		d[ind] = 0
		sum += maxm
	print(sum)


try:
	for _ in range(int(input())):
		(N, M) = input().split()
		a = []
		b = []
		massimo = 0
		pippo = 0
		c = []
		N = int(N)
		M = int(M)
		a = list(map(int, input().split()))
		for i in range(N):
			b = list(map(int, input().split()))
			b.pop(0)
			c.append(b)
		for i in range(M):
			pippo = max(c[a[i]])
			massimo += pippo
			if len(c[a[i]]) > 1:
				c[a[i]].remove(pippo)
			else:
				c[a[i]].remove(pippo)
				c[a[i]].append(0)
		print(massimo)
except EOFError:
	exit()


t = int(input())
while t != 0:
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	d = []
	s = 0
	for i in range(n):
		v = list(map(int, input().split()))
		v = sorted(v[1:], reverse=True)
		d.append(v)
	for i in range(m):
		if len(d[p[i]]) == 0:
			continue
		s += d[p[i]][0]
		d[p[i]] = d[p[i]][1:]
	print(s)
	t -= 1


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	pi = list(map(int, input().split()))[:m]
	sume = 0
	ans = 0
	li = []
	for i in range(n):
		civi = list(map(int, input().split()))
		ci = civi[0]
		vi = civi[1:]
		vi.sort(reverse=True)
		ans = pi.count(i)
		sume += sum(vi[:ans])
	print(sume)


import sys
for i in range(int(sys.stdin.readline())):
	k = list(map(int, sys.stdin.readline().split()))
	p = list(map(int, sys.stdin.readline().split()))
	l = []
	for j in range(k[0]):
		l.append(list(map(int, sys.stdin.readline().split())))
		del l[j][0]
	c = 0
	for r in p:
		c += max(l[r])
		l[r][l[r].index(max(l[r]))] = 0
	sys.stdout.write(str(c) + '\n')


t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	q = list(map(int, input().split()))
	l = []
	for j in range(n):
		k = list(map(int, input().split()))
		l.append(k[1:])
	c = 0
	for j in range(len(q)):
		x = max(l[q[j]])
		c += x
		l[q[j]].remove(max(l[q[j]]))
		l[q[j]].append(0)
	print(c)


import heapq
for i in range(int(input())):
	(n, m) = map(int, input().split())
	P = list(map(int, input().split()))
	l = []
	sum = 0
	for j in range(n):
		t = list(map(int, input().split()))
		t.pop(0)
		heapq.heapify(t)
		t.sort()
		l.append(t)
	for j in P:
		if len(l[j]) != 0:
			sum += l[j].pop()
	print(sum)


t = int(input())
while t > 0:
	(n, m) = map(int, input().split())
	room = list(map(int, input().split()))
	qqq = []
	while n > 0:
		temp = list(map(int, input().split()))
		c = temp[0]
		lst = temp[1:].copy()
		qqq.append(lst)
		n -= 1
	ans = 0
	for i in room:
		if qqq[i] != []:
			ans += max(qqq[i])
			qqq[i].remove(max(qqq[i]))
	print(ans)
	t -= 1


for _ in range(int(input())):
	ltrs = 0
	(n, m) = [int(x) for x in input().split(' ')]
	p = [int(x) for x in input().split(' ')]
	temp = set(p)
	l1 = []
	for __ in range(n):
		arr = [int(x) for x in input().split(' ')]
		del arr[0]
		arr.sort()
		l1.append(arr)
	for i in p:
		if l1[i] != []:
			ltrs += l1[i][-1]
			l1[i].pop(-1)
	print(ltrs)


import heapq
T = int(input())
for t in range(T):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	room = [None for i in range(n)]
	for i in range(n):
		batli = list(map(lambda x: -int(x), input().split()))[1:]
		heapq.heapify(batli)
		room[i] = batli
	elephant = 0
	for r in p:
		if len(room[r]) == 0:
			continue
		bigbatli = -1 * heapq.heappop(room[r])
		elephant += bigbatli
	print(elephant)


for _ in range(int(input())):
	(n, m) = map(int, input().split(' '))
	total = 0
	p = list(map(int, input().split(' ')))
	pl = []
	for __ in range(n):
		pl.append(list(map(int, input().split(' ')))[1:])
	for i in pl:
		i.sort()
	for i in p:
		total += pl[i][-1]
		pl[i][-1] = 0
		pl[i].sort()
	print(total)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	m_list = []
	for j in range(n):
		m_list.append(sorted(list(map(int, input().split()))[1:]))
	total = 0
	for j in l:
		if m_list[j]:
			total += m_list[j].pop()
	print(total)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	arr = list(map(int, input().split()))
	out = []
	for _ in range(n):
		out.append(sorted(list(map(int, input().split()))[1:]))
	ans = 0
	for i in arr:
		ans += out[i][-1]
		out[i][-1] = 0
		out[i].sort()
	print(ans)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	total = 0
	p = list(map(int, input().split()))
	q = []
	for __ in range(n):
		q.append(list(map(int, input().split()))[1:])
	for i in p:
		total += max(q[i])
		ind = q[i].index(max(q[i]))
		q[i][ind] = 0
	print(total)


case = int(input())
for i in range(case):
	(m, n) = map(int, input().split(' '))
	p = list(map(int, input().split(' ')))
	volume = []
	for j in range(m):
		arr = list(map(int, input().split(' ')))
		arr.pop(0)
		arr.sort()
		volume.append(arr)
	ans = 0
	for room in p:
		if len(volume[room]) > 0:
			ans += volume[room].pop()
	print(ans)


t = int(input())
while t > 0:
	t -= 1
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	D = {}
	for i in range(n):
		c = list(map(int, input().split()))
		D[i] = c[1:]
	sum = 0
	for i in range(len(p)):
		x = D[p[i]]
		max = -1
		idx = -1
		for j in range(len(x)):
			if x[j] > max:
				max = x[j]
				idx = j
		D[p[i]][idx] = 0
		sum += max
	print(sum)


from collections import deque
for _ in range(int(input())):
	(n, m) = map(int, input().split())
	r = list(map(int, input().split()))
	aux = {}
	for i in range(n):
		t_1 = list(map(int, input().split()))
		aux[i] = deque(sorted(t_1[1:], reverse=True))
	v = 0
	for i in range(m):
		if len(aux[r[i]]) > 0:
			t_2 = aux[r[i]].popleft()
			v += t_2
	print(v)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	l = [int(i) for i in input().split()]
	arr = []
	for i in range(n):
		arr.append([int(i) for i in input().split()][1:])
	for i in arr:
		i.sort()
	ans = 0
	for i in l:
		if arr[i] == []:
			continue
		else:
			ans += arr[i].pop()
	print(ans)


t = 0
try:
	t = int(input())
except:
	pass
for _ in range(t):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	r = {}
	count = 0
	for i in range(n):
		x = list(map(int, input().split()))
		r[i] = x[1:]
	for i in p:
		z = max(r[i])
		y = r[i].index(z)
		r[i][y] = 0
		count += z
	print(count)


t = int(input())
for k in range(t):
	(n, m) = map(int, input().split())
	rooms = [int(x) for x in input().split()]
	l = []
	s = 0
	for j in range(n):
		a = [int(x) for x in input().split()]
		a.pop(0)
		l.append(a)
	for item in rooms:
		if len(l[item]) != 0:
			m = max(l[item])
			s += m
			l[item].remove(m)
	print(s)


t = int(input())
for i in range(t):
	sum1 = 0
	c = list()
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	for j in range(n):
		x = list(map(int, input().split()))
		c.append(x)
	for j in a:
		largest = -1
		for k in range(c[j][0]):
			if c[j][k + 1] > largest:
				largest = c[j][k + 1]
				temp = k + 1
		sum1 += largest
		c[j][temp] = 0
	print(sum1)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	r = list(map(int, input().split()))
	b = [list(map(int, input().split())) for _ in range(n)]
	for i in range(n):
		a = b[i][1:]
		a.sort()
		b[i][1:] = a
	sum = 0
	for i in range(m):
		if len(b[r[i]]) > 1:
			sum += b[r[i]][len(b[r[i]]) - 1]
			b[r[i]].pop()
	print(sum)


def calc(n, m, p, bottles):
	p_i = 0
	drink = 0
	while p_i < m:
		l_i = 0
		r_i = p[p_i]
		mx = max(bottles[r_i])
		drink += mx
		while l_i < len(bottles[r_i]):
			if bottles[r_i][l_i] == mx:
				bottles[r_i][l_i] = 0
				break
			l_i += 1
		p_i += 1
	return drink
t = int(input())
for _ in range(t):
	inp = input().split()
	n = int(inp[0])
	m = int(inp[1])
	p_str = input().split()
	p = []
	for x in p_str:
		p.append(int(x))
	bottles = []
	k = 0
	while k < n:
		bottles_str = input().split()
		bot = []
		i = 1
		while i < len(bottles_str):
			bot.append(int(bottles_str[i]))
			i += 1
		bottles.append(bot)
		k += 1
	res = calc(n, m, p, bottles)
	print(res)


t = int(input())
for case in range(t):
	volume = 0
	rooms = []
	(n, m) = input().split()
	n = int(n)
	p = list(map(int, input().split()))
	for room in range(n):
		rooms.append(list(map(int, input().split()))[1:])
	for room in rooms:
		room.sort()
	for i in p:
		volume += rooms[i][-1]
		rooms[i][-1] = 0
		rooms[i].sort()
	print(volume)


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	P = list(map(int, input().split()))
	lemon = []
	for _ in range(n):
		lemon.append(list(map(int, input().split())))
		lemon[-1].pop(0)
	drink = 0
	for visit in P:
		if len(lemon[visit]) != 0:
			drink += max(lemon[visit])
			lemon[visit].pop(lemon[visit].index(max(lemon[visit])))
	print(drink)


for __ in range(int(input())):
	(n, m) = map(int, input().split())
	p = list(map(int, input().split()))
	d = {}
	room = 0
	drinks = 0
	for i in range(n):
		c = list(map(int, input().split()))
		e = c[1:]
		e.sort()
		d[room] = e
		room += 1
	for j in p:
		if len(list(d[j])) > 0:
			drinks += d[j].pop()
	print(drinks)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	lis1 = list(map(int, input().split()))
	l = []
	res = 0
	for j in range(n):
		(c, *lp) = map(int, input().split())
		l.append(lp)
	for j in lis1:
		temp = l[j]
		if len(temp) == 0:
			continue
		else:
			find = max(temp)
			res = res + find
			temp.remove(find)
	print(res)


t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	m = []
	r = list(map(int, input().split()))
	for i in range(n):
		l = list(map(int, input().split()))
		l = l[1:]
		m.append(l)
	i = 0
	p = 0
	j = 0
	while j < k:
		if len(m[r[j]]) != 0:
			p = p + max(m[r[j]])
			m[r[j]].remove(max(m[r[j]]))
		j += 1
	print(p)
	t = t - 1


try:
	t: int = int(input())
	for i in range(0, t):
		(n, m) = map(int, input().split())
		p = list(map(int, input().split()))
		rooms: dict = {}
		count: int = 0
		for j in range(n):
			x = list(map(int, input().split()))
			x.pop(0)
			rooms[j] = x
		for j in range(m):
			z: int = p[j]
			if len(rooms[z]) != 0:
				el: int = max(rooms[z])
				count += el
				rooms[z].remove(el)
		print(count)
except:
	pass


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	order = list(map(int, input().split()))
	rooms = {i: list(map(int, input().split()))[1:] for i in range(n)}
	drank = 0
	for i in order:
		if rooms[i]:
			drank += max(rooms[i])
			rooms[i].remove(max(rooms[i]))
	print(drank)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	l = list(map(int, input().split()))
	f = {}
	for j in l:
		if j in f:
			f[j] += 1
		else:
			f[j] = 1
	ans = 0
	for j in range(n):
		s = input()
		if j in f:
			a = list(map(int, s.split()))
			d = sorted(a[1:])
			ans += sum(d[-1 * f[j]:])
	print(ans)