Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.

Mahmoud knows that the i-th word can be sent with cost a_{i}. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message?

The cost of sending the message is the sum of the costs of sending every word in it.


-----Input-----

The first line of input contains integers n, k and m (1 ≤ k ≤ n ≤ 10^5, 1 ≤ m ≤ 10^5) — the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively.

The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct.

The third line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) where a_{i} is the cost of sending the i-th word.

The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group.

The next line contains m space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words.


-----Output-----

The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.


-----Examples-----
Input
5 4 4
i loser am the second
100 1 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

Output
107
Input
5 4 4
i loser am the second
100 20 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

Output
116


-----Note-----

In the first sample, Mahmoud should replace the word "second" with the word "loser" because it has less cost so the cost will be 100+1+5+1=107.

In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116.

Difficulty: EASY
Tags: Spanning trees

Implementation

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
from collections import defaultdict
(n, k, m) = map(int, input().split())
meh = input()
meh = meh.split()
ww = {b: a for (a, b) in enumerate(meh)}
cost = list(map(int, input().split()))
for i in range(k):
	change = list(map(int, input().split()))
	if change[0] > 1:
		mins = min([cost[j - 1] for j in change[1:]])
		for j in change[1:]:
			cost[j - 1] = mins
ehab = input()
ehab = ehab.split()
ans = 0
for i in ehab:
	ans += cost[ww[i]]
print(ans)


(n, k, m) = tuple(map(int, input().split()))
words = input().split()
wtoi = dict()
for i in range(n):
	wtoi[words[i]] = i + 1
costs = list(map(int, input().split()))
for i in range(k):
	words_in_group = list(map(int, input().split()[1:]))
	cost = min([costs[i - 1] for i in words_in_group])
	for w in words_in_group:
		costs[w - 1] = cost
message = list(map(lambda w: wtoi[w], input().split()))
cost = sum([costs[i - 1] for i in message])
print(cost)


(n, k, m) = map(int, input().split())
L = input().split()
A = list(map(int, input().split()))
for i in range(k):
	inp = list(map(int, input().split()))
	X = []
	ind = 1
	for i in range(2, inp[0] + 1):
		if A[inp[i] - 1] < A[inp[ind] - 1]:
			ind = i
	for i in range(1, inp[0] + 1):
		A[inp[i] - 1] = A[inp[ind] - 1]
dic = dict()
for i in range(n):
	dic[L[i]] = A[i]
mes = input().split()
out = 0
for i in mes:
	out += dic[i]
print(out)


from sys import stdin
all_in = stdin.read().split('\n')
(n, k, m) = list(map(int, all_in[0].split()))
dict_ = {s: i for (i, s) in enumerate(all_in[1].split(), 1)}
a = list(map(int, all_in[2].split()))
s = all_in[k + 3].split()
for i in range(3, k + 3):
	(x, *set_) = list(map(int, all_in[i].split()))
	mn = min([a[j - 1] for j in set_])
	for j in set_:
		a[j - 1] = mn
print(sum([a[dict_[el] - 1] for el in s]))


(n, k, m) = map(int, input().split())
s = input().split()
c = list(map(int, input().split()))
d = {}
for i in range(k):
	p = list(map(int, input().split()))[1:]
	minCost = min((c[j - 1] for j in p))
	for j in p:
		d[s[j - 1]] = minCost
res = input().split()
print(sum((d[i] for i in res)))


import itertools
from logging import getLogger, DEBUG
logger = getLogger(__name__)
logger.setLevel(DEBUG)

def get_min_cost(word_dict, message_words, min_group_cost):
	cost = 0
	for word in message_words:
		group_idx = word_dict[word]['group']
		cost += min_group_cost[group_idx]
	return cost
(n, k, m) = [int(num) for num in input().split()]
words = [word for word in input().split()]
cost = [int(c) for c in input().split()]
word_dict = dict()
min_group_cost = [0] * k
for (word, word_cost) in zip(words, cost):
	word_dict[word] = dict(cost=word_cost)
for i in range(k):
	min_cost = pow(10, 9) + 1
	for idx in input().split()[1:]:
		word = words[int(idx) - 1]
		word_dict[word]['group'] = i
		min_cost = min(min_cost, word_dict[word]['cost'])
	min_group_cost[i] = min_cost
message_words = [word for word in input().split()]
print(get_min_cost(word_dict=word_dict, message_words=message_words, min_group_cost=min_group_cost))


(n, k, m) = map(int, input().split())
a = input().split()
numbs = dict()
for i in range(n):
	numbs[a[i]] = i + 1
groups = [[] for i in range(k)]
cost = [int(x) for x in input().split()]
for i in range(k):
	groups[i] = set(map(int, input().split()[1:]))
mes = input().split()
nm = [numbs[mes[i]] for i in range(m)]
a = [0] * n
for i in range(m):
	a[nm[i] - 1] += 1
mins = [1000000001] * k
for i in range(k):
	for u in groups[i]:
		mins[i] = min(mins[i], cost[u - 1])
	for u in groups[i]:
		cost[u - 1] = mins[i]
ans = 0
for i in range(n):
	ans += cost[i] * a[i]
print(ans)


(n, k, m) = map(int, input().split())
dic = input().split()
for i in range(n):
	dic[i] = [dic[i], i]
dic = dict(dic)
pr = list(map(int, input().split()))
for i in range(k):
	arr = list(map(int, input().split()))[1:]
	mm = 1000000000
	for j in arr:
		mm = min(mm, pr[j - 1])
	for j in arr:
		pr[j - 1] = mm
s = input().split()
res = 0
for i in s:
	res += pr[dic[i]]
print(res)


(n, k, m) = map(int, input().split())
lang = input().split()
cost = list(map(int, input().split()))
groups = {}
mincost = {}
for g in range(k):
	(x, *ind) = map(int, input().split())
	for index in ind:
		groups[lang[index - 1]] = g + 1
	m = 10000000000
	for index in ind:
		if cost[index - 1] < m:
			m = cost[index - 1]
	mincost[g + 1] = m
message = input().split()
total = 0
for word in message:
	total += mincost[groups[word]]
print(total)


(ip, rd) = (lambda : input().split(), lambda : map(int, ip()))
(n, k, m) = rd()
a = ip()
d = dict(zip(a, rd()))
for _ in range(k):
	x = list(rd())[1:]
	m = min((d[a[i - 1]] for i in x))
	for i in x:
		d[a[i - 1]] = m
print(sum((d[x] for x in ip())))


(n, k, m) = map(int, input().split())
words = input().split()
strings = {}
for i in range(n):
	strings[words[i]] = 0
cost = list(map(int, input().split()))
for i in range(k):
	array = list(map(int, input().split()))
	if array[0] == 1:
		strings[words[array[1] - 1]] = cost[array[1] - 1]
	else:
		mini = cost[array[1] - 1]
		for j in range(1, array[0] + 1):
			if cost[array[j] - 1] < mini:
				mini = cost[array[j] - 1]
		for j in range(1, array[0] + 1):
			strings[words[array[j] - 1]] = mini
word = input().split()
total = 0
for i in range(m):
	total += strings[word[i]]
print(total)


Q = input().split()
n = int(Q[0])
k = int(Q[1])
n = int(Q[2])
a = input().split()
L1 = list(map(int, input().split()))
sumL1 = sum(L1)
print()
D = dict()
for i in range(k):
	L2 = list(map(int, input().split()))
	min1 = 99999999999
	for j in range(1, L2[0] + 1):
		if L1[L2[j] - 1] < min1:
			min1 = L1[L2[j] - 1]
	for j in range(1, L2[0] + 1):
		if L1[L2[j] - 1] != min1:
			L1[L2[j] - 1] = min1
for i in range(len(a)):
	D[a[i]] = L1[i]
c = input().split()
sum1 = 0
for i in c:
	sum1 += D[i]
print(sum1)


(n, k, m) = map(int, input().split())
words = [i for i in input().split()]
d1 = {}
for i in range(len(words)):
	d1[words[i]] = i
cost = [int(i) for i in input().split()]
parent = [-1] * 100005
d = {}
from collections import defaultdict
d = defaultdict(list)
mind = {}
for _ in range(k):
	minim = 10 ** 10
	grp = [int(i) - 1 for i in input().split()]
	l = grp
	for i in range(1, len(grp)):
		parent[l[i]] = l[1]
		if cost[l[i]] < minim:
			minim = cost[l[i]]
	mind[l[1]] = minim
res = 0
msg = [i for i in input().split()]
for i in msg:
	res += mind[parent[d1[i]]]
print(res)


(n, k, m) = (int(x) for x in input().split())
raw_words = input().split()
weights = list((int(x) for x in input().split()))
words = {}
for (i, w) in enumerate(raw_words):
	words[i] = {'word': w, 'weight': weights[i]}
g = {}
for i in range(k):
	ga = list((int(x) for x in input().split()[1:]))
	gw = min(list((words[x - 1]['weight'] for x in ga)))
	for j in ga:
		g[words[j - 1]['word']] = gw
res = input().split()
r = 0
for word in res:
	r += g[word]
print(r)


(n, k, m) = map(int, input().split())
a = list(input().split())
b = list(map(int, input().split()))
while k:
	k -= 1
	o = list(input().split())
	x = int(o[0])
	c = list(map(int, o[1:]))
	mini = min([b[i - 1] for i in c])
	for i in c:
		b[i - 1] = mini
p = list(input().split())
ma = {}
for i in range(len(a)):
	ma[a[i]] = b[i]
s = 0
for i in p:
	s += ma[i]
print(s)


a = list(map(int, input().split(' ')))
(n, k, m) = (a[0], a[1], a[2])
ttt = 2
words = list(input().split(' '))
cost = list(map(int, input().split(' ')))
dic = {}
for i in range(k):
	tmp = list(map(int, input().split(' ')))
	if tmp[0] == 1:
		dic[words[tmp[1] - 1]] = cost[tmp[1] - 1]
	else:
		mincost = min(cost[tmp[1] - 1], cost[tmp[2] - 1])
		for j in range(3, tmp[0] + 1):
			mincost = min(mincost, cost[tmp[j] - 1])
		for p in range(1, tmp[0] + 1):
			dic[words[tmp[p] - 1]] = mincost
message = list(input().split(' '))
res = 0
for word in message:
	res += dic[word]
print(res)


import math
(n, k, m) = map(int, input().split())
word_map = dict(zip(input().split(), range(n)))
cost = list(map(int, input().split()))
cost_d = {}
for i in range(k):
	groups = list(map(int, input().split()))
	mn = math.inf
	for g in groups[1:]:
		mn = min(mn, cost[g - 1])
	for g in groups[1:]:
		cost_d[g - 1] = mn
words = input().split()
ans = 0
for w in words:
	ans += cost_d[word_map[w]]
print(ans)


(n, k, m) = [int(x) for x in input().split()]
w = [x for x in input().split()]
a = [int(x) for x in input().split()]
cost = {w[i]: a[i] for i in range(n)}
group = {}
minis = []
for i in range(k):
	inp = [int(x) - 1 for x in input().split()]
	mini = 10000000000
	mini_w = None
	for j in range(1, inp[0] + 2):
		if cost[w[inp[j]]] < mini:
			mini_w = cost[w[inp[j]]]
			mini = cost[w[inp[j]]]
		group[w[inp[j]]] = i
	minis.append(mini_w)
letter = [x for x in input().split()]
summa = 0
for wor in letter:
	summa += minis[group[wor]]
print(summa)


tmp = input().split()
n = int(tmp[0])
k = int(tmp[1])
m = int(tmp[2])
words = []
tmp = input().split()
for i in tmp:
	words.append(i)
cost = []
tmp = input().split()
for i in tmp:
	cost.append(int(i))
for i in range(k):
	tmp = input().split()
	s = 0
	min = 1000000009
	for j in tmp:
		if s != 0:
			if min > cost[int(j) - 1]:
				min = cost[int(j) - 1]
		s += 1
	s = 0
	for j in tmp:
		if s != 0:
			cost[int(j) - 1] = min
		s += 1
dict = {}
for i in range(n):
	dict[words[i]] = cost[i]
tmp = input().split()
s = 0
for i in tmp:
	s += dict[i]
print(s)


(n, k, m) = map(int, input().split())
words = input().split()
costs = map(int, input().split())
words_costs = dict(zip(words, costs))
for i in range(k):
	sin_word = list(map(int, input().split()))[1:]
	min_costs = min((words_costs[words[j - 1]] for j in sin_word))
	for j in sin_word:
		words_costs[words[j - 1]] = min_costs
print(sum((words_costs[w] for w in input().split())))


class Node(object):

	def __init__(self, label):
		self.label = label
		self.par = self
		self.rank = 0
		self.min = 0

class DisjointSet(object):

	def __init__(self, n):
		self.n = n
		self.nodes = [Node(i) for i in range(n)]

	def find(self, u):
		if u == u.par:
			return u
		return self.find(u.par)

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if u.rank > v.rank:
			(u, v) = (v, u)
		u.par = v
		if u.rank == v.rank:
			v.rank += 1
		v.min = min(v.min, u.min)
		return True

	def get_all_components(self):
		comps = [[] for _ in range(self.n)]
		for node in self.nodes:
			comps[self.find(node).label].append(node.label)
		comps = [i for i in comps if i]
		return comps

def solve(n, m, words, prices, groups, message):
	dsu = DisjointSet(n)
	indices = {}
	for i in range(n):
		dsu.nodes[i].min = prices[i]
		indices[words[i]] = i
	for group in groups:
		for i in range(len(group)):
			dsu.union(dsu.nodes[group[0]], dsu.nodes[group[i]])
	price = 0
	for word in message:
		price += dsu.find(dsu.nodes[indices[word]]).min
	return price
(n, k, m) = map(int, input().split(' '))
words = [x for x in input().split(' ')]
prices = [int(x) for x in input().split(' ')]
groups = []
for i in range(k):
	l = [int(x) - 1 for x in input().split(' ')]
	groups.append(l[1:])
message = input().split(' ')
print(solve(n, m, words, prices, groups, message))


from sys import stdin, stdout
import math, sys, heapq
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import random
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

	def I():
		return int(stdin.readline())

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return -1

def main():
	try:
		(n, k, m) = In()
		T = list(input().split())
		cost = list(In())
		d = {}
		for x in range(k):
			temp = list(In())
			zara = []
			co = 10 ** 18
			for t in range(1, temp[0] + 1):
				zara.append(T[temp[t] - 1])
				co = min(cost[temp[t] - 1], co)
			for t in zara:
				d[t] = co
		M = list(input().split())
		ans = 0
		for x in M:
			ans += d[x]
		print(ans)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(1):
	main()


(n, k, m) = map(int, input().strip().split())
nw = list(input().split())
nc = list(map(int, input().split()))
d = dict()
for i in range(k):
	l = list(map(int, input().strip().split()))[1:]
	mn = min((nc[j - 1] for j in l))
	for j in l:
		d[nw[j - 1]] = mn
mw = list(input().split())
mc = 0
for i in mw:
	mc += d[i]
print(mc)


(n, k, m) = map(int, input().split())
s = list(input().split())
l = list(map(int, input().split()))
for i in range(k):
	g = list(map(int, input().split()))
	if g[0] > 1:
		mini = l[g[1] - 1]
		for x in g[1:]:
			if l[x - 1] < mini:
				mini = l[x - 1]
		for x in g[1:]:
			l[x - 1] = mini
(s, l) = zip(*sorted(zip(s, l)))
words = list(input().split())
words.sort()
count = 0
ptr = 0
for x in words:
	while x != s[ptr]:
		ptr += 1
	if x == s[ptr]:
		count += l[ptr]
print(count)


import os
import sys
from io import BytesIO, IOBase
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')
import math

def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
ans = 0
(n, k, m) = map(int, input().split())
language = dict()
lang = list(input().split())
indices = [-1] * n
for i in range(n):
	language.update({lang[i]: i})
costs = list(map(int, input().split()))
groupcost = [0] * k
for i in range(k):
	mini = 10000000000
	group = list(map(int, input().split()))
	for j in range(1, group[0] + 1):
		if costs[group[j] - 1] < mini:
			mini = costs[group[j] - 1]
		indices[group[j] - 1] = i
	groupcost[i] = mini
message = list(input().split())
for i in range(m):
	ans += groupcost[indices[language[message[i]]]]
print(ans)


s = input().split()
(n, k, m) = (int(s[0]), int(s[1]), int(s[2]))
s = input().split()
d = dict()
for i in range(len(s)):
	d[s[i]] = i + 1
priceold = input().split()
price = ['ZERONUM'] + list(map(int, priceold))
for i in range(k):
	sold = input().split()
	s = list(map(int, sold))
	minimalpoint = price[s[1]]
	for j in range(2, len(s)):
		minimalpoint = min(minimalpoint, price[s[j]])
	for j in range(1, len(s)):
		price[s[j]] = minimalpoint
count = 0
words = input().split()
for i in range(m):
	number = d[words[i]]
	count += price[number]
print(count)


def root(a):
	while parent[a] != a:
		parent[a] = parent[parent[a]]
		a = parent[a]
	return a

def union(a, b):
	a = root(a)
	b = root(b)
	if a == b:
		return
	if cost[root(a) - 1] <= cost[root(b) - 1]:
		parent[b] = a
	else:
		parent[a] = b
from collections import defaultdict
(n, k, m) = map(int, input().split())
lang = input().split()
j = 1
word = {}
for c in lang:
	word[c] = j
	j += 1
parent = [i for i in range(n + 1)]
cost = [int(e) for e in input().split()]
grp = {}
for _ in range(k):
	new = [int(e) for e in input().split()]
	for e in new[1:]:
		union(e, new[1])
mess = input().split()
mess = [word[c] for c in mess]
ans = 0
for w in mess:
	ans += cost[root(w) - 1]
print(ans)


(n, k, m) = map(int, input().split())
d = {}
seq = input().split()
cost = list(map(int, input().split()))
for i in range(n):
	d[seq[i]] = i
for i in range(k):
	sq = list(map(lambda x: x - 1, map(int, input().split())))
	x = sq.pop(0)
	mn = cost[sq[0]]
	for j in range(x + 1):
		if cost[sq[j]] < mn:
			mn = cost[sq[j]]
	for j in range(x + 1):
		cost[sq[j]] = mn
msg = input().split()
ans = 0
for t in msg:
	ans += cost[d[t]]
print(ans)


def main():
	(n, k, m) = map(int, input().split())
	words = input().split()
	cost = list(map(int, input().split()))
	groups = {}
	group_costs = [0] * k
	for i in range(k):
		ws = list(map(lambda w: int(w) - 1, input().split()[1:]))
		x = map(lambda w: cost[w], ws)
		for w in ws:
			groups[words[w]] = i
		group_costs[i] = min(x)
	mesg = input().split()
	print(sum(list(map(lambda w: group_costs[groups[w]], mesg))))
main()


(n, k, m) = list(map(int, input().split()))
word_tree = {}
keys = list(input().split())
weights = list(map(int, input().split()))
for i in range(k):
	group = list(map(int, input().split()))
	g = []
	min_g = group[1] - 1
	for j in group[1:]:
		g.append(j - 1)
		min_g = j - 1 if weights[j - 1] < weights[min_g] else min_g
	for j in g:
		word_tree[keys[j]] = weights[min_g]
message = list(input().split())
cost = 0
for i in message:
	cost += word_tree[i]
print(cost)


a = input().split(' ')
n = int(a[0])
k = int(a[1])
m = int(a[2])
words = input().split(' ')
cost = input().split(' ')
x = {}
for i in range(0, k):
	q = input().split(' ')
	if int(q[0]) > 1:
		min = int(cost[int(q[1]) - 1])
		for z in range(2, int(q[0]) + 1):
			d = int(cost[int(q[z]) - 1])
			if d < min:
				min = d
		for e in range(1, int(q[0]) + 1):
			x.update({words[int(q[e]) - 1]: min})
	else:
		x.update({words[int(q[1]) - 1]: cost[int(q[1]) - 1]})
price = 0
p = input().split(' ')
for r in range(m):
	price = price + int(x[p[r]])
print(str(price))


(n, k, m) = list(map(int, input().split()))
w = input().split()
a = list(map(int, input().split()))
d = dict(list(zip(w, a)))
for i in range(k):
	s = list(map(int, input().split()[1:]))
	m = min((d[w[x - 1]] for x in s))
	for x in s:
		d[w[x - 1]] = m
print(sum((d[x] for x in input().split())))


ii = lambda : int(input())
kk = lambda : map(int, input().split())
ll = lambda : list(kk())
(n, k, m) = kk()
(words, vals) = (input().split(), ll())
d = {words[i]: i for i in range(n)}
for _ in range(k):
	inps = ll()[1:]
	minv = vals[inps[0] - 1]
	for v in inps:
		minv = min(minv, vals[v - 1])
	for v in inps:
		vals[v - 1] = minv
cnt = 0
for w in input().split():
	cnt += vals[d[w]]
print(cnt)


(n, k, m) = map(int, input().split())
(w, a, d) = (input().split(), list(map(int, input().split())), {})
for i in range(k):
	g = list(map(int, input().split()))[1:]
	c = min((a[gi - 1] for gi in g))
	for gi in g:
		d[w[gi - 1]] = c
print(sum((d[x] for x in input().split())))


(n, k, m) = list(map(int, input().split()))
voc = input().split()
al = list(map(int, input().split()))
gl = [list(map(int, input().split())) for _ in range(k)]
wl = input().split()
w2i = {voc[i]: i for i in range(n)}
g2c = [min([al[i - 1] for i in g[1:]]) for g in gl]
i2g = [None] * n
for (gi, g) in enumerate(gl):
	for i in g[1:]:
		i2g[i - 1] = gi
cost = sum([g2c[i2g[w2i[w]]] for w in wl])
print(cost)


(n, k, m) = list(map(int, input().split()))
words = input().split()
n_word = dict()
for (id, word) in enumerate(words):
	n_word[word] = id
costs = list(map(int, input().split()))
groups = []
for i in range(k):
	groups.append(list(map(int, input().split()))[1:])
n_gr = dict()
gr_costs = []
for i in range(k):
	for n_wrd in groups[i]:
		n_gr[n_wrd - 1] = i
	cur_gr_costs = [costs[b - 1] for b in groups[i]]
	gr_costs.append(min(cur_gr_costs))
soobsh = input().split()
res = 0
for word in soobsh:
	res += gr_costs[n_gr[n_word[word]]]
print(res)


(n, k, m) = map(int, input().split())
s = list(input().split())
cost = list(map(int, input().split()))
id = dict()
for i in range(n):
	id[s[i]] = i
for i in range(k):
	t = list(map(int, input().split()))
	minn = cost[t[1] - 1]
	for j in range(1, t[0] + 1):
		minn = min(minn, cost[t[j] - 1])
	for j in range(1, t[0] + 1):
		cost[t[j] - 1] = minn
s0 = list(input().split())
ans = 0
for i in range(m):
	ans += cost[id[s0[i]]]
print(int(ans))


(a, b, c) = map(int, input().split())
l = list(map(str, input().split()))
m = list(map(int, input().split()))
k = 0
D = {}
for i in range(a):
	D[l[i]] = m[i]
for i in range(b):
	k = list(map(int, input().split()))
	minimum = m[k[1] - 1]
	for i in range(1, k[0] + 1):
		if minimum > m[k[i] - 1]:
			minimum = m[k[i] - 1]
	for i in range(1, k[0] + 1):
		D[l[k[i] - 1]] = minimum
count = 0
l = list(map(str, input().split()))
for i in l:
	count += D[i]
print(count)


def main():
	nkm = input()
	nkm = nkm.split()
	n = int(nkm[0])
	k = int(nkm[1])
	m = int(nkm[2])
	words = input()
	words = words.split()
	costs = input()
	costs = costs.split()
	groups = list()
	dic = dict()
	for i in range(k):
		member = input()
		member = member.split()
		group = list()
		for j in range(int(member[0])):
			index = int(member[j + 1])
			dic[words[index - 1]] = i
			group.append(int(costs[index - 1]))
		group.sort()
		groups.append(group)
	send = input()
	send = send.split()
	ans = 0
	for w in send:
		ans += groups[dic[w]][0]
	print(ans)
main()


(n, k, m) = map(int, input().strip().split())
word_voc = input().strip().split()
cost_dic = dict(((v, 0) for v in word_voc))
costs = list(map(int, input().strip().split()))
for _ in range(k):
	groups = list(map(int, input().strip().split()))
	min_val = float('inf')
	for i in groups[1:]:
		min_val = min(min_val, costs[i - 1])
	for i in groups[1:]:
		cost_dic[word_voc[i - 1]] = min_val
love = input().strip().split()
res = 0
for s in love:
	res += cost_dic[s]
print(res)


I = lambda : input().split()
R = lambda : list(map(int, I()))
(n, k, m) = R()
w = dict(zip(I(), range(n)))
a = R()
for x in [R()[1:] for _ in [0] * k]:
	c = min((a[i - 1] for i in x))
	for i in x:
		a[i - 1] = c
print(sum((a[w[x]] for x in I())))


(n, k, m) = map(int, input().strip().split())
a = input().strip().split()
p = list(map(int, input().strip().split()))
priceTable = {}
for i in range(n):
	priceTable[a[i]] = p[i]
for i in range(k):
	line = list(map(int, input().split()))
	x = line[0]
	indices = line[1:]
	if x > 1:
		minPrice = min(list(map(lambda i: p[i - 1], indices)))
		for index in indices:
			priceTable[a[index - 1]] = minPrice
costs = [priceTable[w] for w in input().strip().split()]
print(sum(costs))


import sys, math
(n, k, m) = map(int, input().strip().split(' '))
langword = list(input().strip().split(' '))
langwordcost = list(map(int, input().strip().split(' ')))
langdict = dict(zip(langword, langwordcost))
for i in range(k):
	sameword = list(map(int, input().strip().split(' ')))
	nofsameelem = sameword.pop(0)
	if nofsameelem == 1:
		continue
	maxpos = max(sameword)
	mini = sys.maxsize
	for pos in sameword:
		mini = min(mini, langwordcost[pos - 1])
	for pos in sameword:
		langdict[langword[pos - 1]] = mini
sendword = list(input().strip().split(' '))
coust = 0
for word in sendword:
	coust += langdict[word]
print(coust)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def main():
	(n, k, m) = [int(x) for x in input().split()]
	s = input().split()
	mm = {}
	a = [int(x) for x in input().split()]
	group = []
	for i in range(k):
		ii = [int(x) - 1 for x in input().split()][1:]
		mc = min((a[x] for x in ii))
		for iii in ii:
			mm[s[iii]] = mc
	print(sum((mm[x] for x in input().split())))

def __starting_point():
	main()
__starting_point()


def main():
	(n, k, m) = map(int, input().split())
	words = ['Zero'] + input().split()
	cost = dict(zip(words, [0] + [int(i) for i in input().split()]))
	for i in range(k):
		(size, *group) = map(int, input().split())
		l = [cost[words[i]] for i in group]
		mincost = min(l)
		for i in group:
			cost[words[i]] = mincost
	message = input().split()
	ans = sum([cost[w] for w in message])
	print(ans)
main()


(n, k, m) = map(int, input().split())
s = list(input().split())
cost = list(map(int, input().split()))
(d, words) = ({}, {})
for i in range(len(s)):
	words.update({s[i]: i})
	words.update({i: s[i]})
for _ in range(k):
	a = list(map(int, input().split()))
	l = []
	for i in range(1, a[0] + 1):
		x = words[a[i] - 1]
		l.append((cost[a[i] - 1], x))
	l.sort()
	c = l[0][0]
	for i in l:
		x = i[1]
		d.update({x: c})
w = list(input().split())
c = 0
for i in w:
	c += d[i]
print(c)


(a, b, c) = map(int, input().split())
z = list(input().split())
z1 = list(map(int, input().split()))
d = dict()
for i in range(b):
	r = list(map(int, input().split()[1:]))
	p = pow(10, 9) + 5
	for j in r:
		p = min(p, z1[j - 1])
	for j in r:
		d[z[j - 1]] = p
print(sum((d[i] for i in input().split())))


Sum = 0
p = []
(n, k, m) = map(int, input().split())
words = list(map(str, input().split()))
worth = list(map(int, input().split()))
for i in range(k):
	x = list(map(int, input().split()))
	if x[0] > 1:
		x.pop(0)
		for bar in x:
			p.append(worth[int(bar) - 1])
		y = min(p)
		for foo in x:
			worth[int(foo) - 1] = y
	p = []
dictionary = dict(zip(words, worth))
s = list(map(str, input().split()))
for a in s:
	Sum += dictionary.get(a)
print(Sum)


(n, k, m) = map(int, input().split())
a = list(input().split())
ind = dict()
for i in range(n):
	ind[a[i]] = i + 1
l = [0] + list(map(int, input().split()))
d = dict()
for y in range(k):
	b = list(map(int, input().split()))
	for j in range(1, len(b)):
		d[b[j]] = b[1]
		l[b[1]] = min(l[b[1]], l[b[j]])
b = list(input().split())
ans = 0
for i in range(m):
	j = ind[b[i]]
	ans += l[d[j]]
print(ans)


import sys
import math
from functools import reduce
import bisect

def getN():
	return int(input())

def getNM():
	return map(int, input().split())

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

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

def index(a, x):
	i = bisect.bisect_left(a, x)
	if i != len(a) and a[i] == x:
		return i
	return False
(n, k, m) = getNM()
words = input().split()
prices = getList()
inds = dict()
teams = dict()
for i in range(k):
	s = getList()[1:]
	teams[i + 1] = 10 ** 9
	for j in s:
		inds[words[j - 1]] = i + 1
		teams[i + 1] = min(prices[j - 1], teams[i + 1])
s = input().split()
ans = 0
for x in s:
	ans += teams[inds[x]]
print(ans)


(total, groups, message) = [int(x) for x in input().split()]
s = [*map(str, input().split())]
cost = [*map(int, input().split())]
price = dict()
for i in range(0, total):
	price[s[i]] = cost[i]
for i in range(0, groups):
	stringList = list(map(int, input().split()))
	groupSize = int(stringList[0])
	mini = 10 ** 9 + 1
	for ptr in range(0, groupSize):
		mini = min(mini, price[s[stringList[ptr + 1] - 1]])
	for ptr in range(0, groupSize):
		price[s[stringList[ptr + 1] - 1]] = mini
message = input()
res = 0
for x in list(message.split()):
	res += price[x]
print(res)


l = input().split(' ')
n = int(l[0])
k = int(l[1])
m = int(l[2])
L = input().split(' ')
M = input().split(' ')
for i in range(len(M)):
	M[i] = int(M[i])
G = [0] * k
dico = {}
for i in range(k):
	t = input().split(' ')
	G[i] = M[int(t[1]) - 1]
	dico[L[int(t[1]) - 1]] = i
	for j in range(2, len(t)):
		dico[L[int(t[j]) - 1]] = i
		if M[int(t[j]) - 1] < G[i]:
			G[i] = M[int(t[j]) - 1]
s = input().split(' ')
c = 0
for e in s:
	c += G[dico[e]]
print(c)


def get_encoded_message(dic, message, weights):
	weight = 0
	for word in message:
		group_id = dic[word]
		weight += weights[group_id]
	return weight
(n, k, m) = map(int, input().split())
words = input().split()[:n]
weights = list(map(int, input().split()))
groups = []
dic = {}
for i in range(k):
	min_val = 10000000000.0
	values = list(map(int, input().split()))
	for j in range(1, values[0] + 1):
		if weights[values[j] - 1] < min_val:
			min_val = weights[values[j] - 1]
		dic[words[values[j] - 1]] = i
	groups.append(min_val)
message = input().split()
print(get_encoded_message(dic, message, groups))


mode = input()
(n, k, m) = [int(temp) for temp in mode.split(' ')]
alphabet = input().split(' ')
cost = input().split(' ')
tmp_zip = zip(alphabet, map(int, cost))
dict_of_cost = dict(tmp_zip)
dict_of_index = list(enumerate(alphabet, 1))
for i in range(0, k):
	mode = list(map(int, input().split(' ')))[1:]
	min = dict_of_cost[dict_of_index[mode[0] - 1][1]]
	for j in mode:
		if dict_of_cost[dict_of_index[j - 1][1]] < min:
			min = dict_of_cost[dict_of_index[j - 1][1]]
	for j in mode:
		dict_of_cost[dict_of_index[j - 1][1]] = min
res_sum = 0
message = input().split(' ')
for msg in message:
	res_sum += dict_of_cost[msg]
print(res_sum)


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
sys.setrecursionlimit(10 ** 5)
from queue import PriorityQueue
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
(n, k, m) = map(int, input().split())
s = input().split(' ')
l = list(map(int, input().split()))
hash = defaultdict(list)
group = defaultdict(int)
for i in range(k):
	la = list(map(int, input().split()))
	x = la[0]
	for j in range(1, len(la)):
		hash[i].append(l[la[j] - 1])
		group[s[la[j] - 1]] = i
	hash[i].sort()
s1 = input().split(' ')
ans = 0
for i in s1:
	z = group[i]
	k = hash[z][0]
	ans += k
print(ans)


(n, k, m) = map(int, input().split())
d1 = {}
acc = 0
for x in input().split():
	d1[x] = acc
	acc += 1
array = []
for x in input().split():
	array.append(int(x))
mass = []
for x in range(k):
	mass.append([])
d2 = {}
for x in range(k):
	temp = list(map(int, input().split()))
	for y in temp[1:]:
		mass[x].append(array[y - 1])
		d2[y - 1] = x
	mass[x].sort()
ans = 0
for x in input().split():
	ans += mass[d2[d1[x]]][0]
print(ans)


(cost_tot, groups) = (0, [])
(n, k, m) = [int(i) for i in input().split()]
alphabet = [i for i in input().split()]
(cost, cost_min) = ({}, {})
for (i, item) in enumerate(input().split()):
	cost[alphabet[i]] = int(item)
for i in range(k):
	aux = input().split()
	aux.pop(0)
	groups.append([int(j) for j in aux])
for i in groups:
	(partial_min, index) = (float('inf'), -1)
	for j in i:
		if cost[alphabet[j - 1]] < partial_min:
			partial_min = cost[alphabet[j - 1]]
			index = j - 1
	for j in i:
		cost_min[alphabet[j - 1]] = cost[alphabet[index]]
message = [i for i in input().split()]
for i in message:
	cost_tot += cost_min[i]
print(cost_tot)


(n, k, m) = [int(x) for x in input().split()]
words = [x for x in input().split()]
costs = [int(x) for x in input().split()]
sets = [set() for x in range(k)]
for i in range(k):
	nums = [int(x) for x in input().split()]
	for j in range(nums[0]):
		sets[i].add(nums[j + 1])
for i in range(k):
	if len(sets[i]) == 1:
		continue
	mincost = 1000000000.0 + 1
	for x in sets[i]:
		mincost = min(costs[x - 1], mincost)
	for x in sets[i]:
		costs[x - 1] = mincost
sends = [x for x in input().split()]
dic = {}
index = 0
for s in words:
	dic[s] = index
	index += 1
cost = 0
for s in sends:
	cost += costs[dic[s]]
print(cost)


from sys import stdin

def main():
	(n, m, k) = map(int, input().split())
	(l, d) = ([0], {})
	for (i, w, a) in zip(range(1, n + 1), input().split(), map(int, input().split())):
		l.append(a)
		d[w] = i
	data = stdin.read().splitlines()
	message = list(map(d.get, data.pop().split()))
	(f, r) = (l.__getitem__, 0)
	for s in data:
		t = list(map(int, s.split()[1:]))
		a = min(map(f, t))
		for i in t:
			l[i] = a
	print(sum(map(l.__getitem__, message)))
main()


(n, k, m) = [int(x) for x in input().split()]
s = input().split()
cost = list(map(int, input().split()))
for i in range(k):
	temp = list(map(int, input().split()))
	x = temp[0]
	b = temp[1:]
	minm = 10 ** 9
	for i in range(x):
		if cost[b[i] - 1] <= minm:
			minm = cost[b[i] - 1]
	for i in range(x):
		cost[b[i] - 1] = minm
start = {s[x]: cost[x] for x in range(n)}
final_cost = 0
messeg = input().split()
for i in range(m):
	final_cost += start[messeg[i]]
print(final_cost)


import sys
input = sys.stdin.readline

class DSU:

	def __init__(self, nodes):
		self.p = [i for i in nodes]
		self.r = [0 for i in nodes]
		self.m = [a[i] for i in nodes]

	def get(self, u):
		if u != self.p[u]:
			self.p[u] = self.get(self.p[u])
		return self.p[u]

	def union(self, u, v):
		u = self.get(u)
		v = self.get(v)
		if u != v:
			if self.r[u] > self.r[v]:
				(u, v) = (v, u)
			self.p[u] = v
			if self.r[u] == self.r[v]:
				self.r[v] += 1
			self.m[v] = min(self.m[u], self.m[v])

	def get_min(self, u):
		u = self.p[u]
		return self.m[u]
(n, k, m) = [int(x) for x in input().split(' ')]
s = input().strip().split(' ')
d = {s[i]: i for i in range(n)}
a = [int(x) for x in input().split(' ')]
dsu = DSU(list(range(n)))
for query in range(k):
	c = [int(x) for x in input().split(' ')]
	if len(c) > 2:
		for z in c[2:]:
			dsu.union(c[1] - 1, z - 1)
ans = sum([dsu.get_min(d[w]) for w in input().strip().split(' ')])
print(ans)


import sys
sys.setrecursionlimit(10000000)

def FIND(x):
	global father
	if x == father[x]:
		return x
	father[x] = FIND(father[x])
	return father[x]

def UNION(x, y):
	global father, minimum, peso
	one = FIND(x)
	two = FIND(y)
	if peso[one] < peso[two]:
		peso[two] += 1
		father[one] = two
		minimum[two] = min(minimum[two], minimum[one])
	else:
		peso[one] += 1
		father[two] = one
		minimum[one] = min(minimum[two], minimum[one])
try:
	(n, k, m) = [int(i) for i in input().split()]
	palavras = {}
	j = 0
	for i in input().split():
		palavras[i] = j
		j += 1
	father = [i for i in range(n)]
	peso = [0 for i in range(n)]
	minimum = [int(i) for i in input().split()]
	i = 0
	while i < k:
		grupos = [int(k) for k in input().split()]
		j = 0
		while j < grupos[0] - 1:
			UNION(grupos[j + 1] - 1, grupos[j + 2] - 1)
			j += 1
		i += 1
	frase = [i for i in input().split()]
	m = 0
	for i in frase:
		x = FIND(palavras[i])
		m += minimum[FIND(palavras[i])]
	print(m)
except RuntimeError:
	print('memoria')


import sys
input = sys.stdin.readline
from collections import defaultdict as dc
from collections import Counter
from bisect import bisect_right, bisect_left
import math
from operator import itemgetter
from heapq import heapify, heappop, heappush
from queue import PriorityQueue as pq
(n, k, m) = map(int, input().split())
l = list(input().split())
f = dc(int)
for i in range(n):
	f[l[i]] = i + 1
c = list(map(int, input().split()))
x = dc(int)
y = dc(int)
for i in range(k):
	p = list(map(int, input().split()))
	for j in p[1:]:
		y[j] = i
		if x[i] == 0:
			x[i] = c[j - 1]
		else:
			x[i] = min(x[i], c[j - 1])
t = list(input().split())
c = 0
for s in t:
	i = f[s]
	g = y[i]
	c += x[g]
print(c)


(n, k, m) = map(int, input().split())
words = list(input().split())
price = list(map(int, input().split()))
dict_wTop = {}
for i in range(n):
	dict_wTop[words[i]] = price[i]
l = []
for i in range(k):
	a = list(map(int, input().split()))
	if len(a) > 2:
		l.append(a)
message = input().split()
rep1 = []
X = []
for e in l:
	Z = []
	rep = []
	e.pop(0)
	for x in e:
		Z.append(words[x - 1])
		rep.append((words[x - 1], price[x - 1]))
	rep1.append(rep)
	X.append(Z)
sorted_rep1 = []
for e in rep1:
	sorted_rep1.append(sorted(e, key=lambda x: x[1]))
set_list = []
for e in X:
	set_list.append(set(e))
b = set(message)
K = []
for e in sorted_rep1:
	K.append(e[0][1])
j = 0
for e in X:
	for i in e:
		dict_wTop[i] = K[j]
	j += 1
sum = 0
for e in message:
	sum += dict_wTop[e]
print(sum)


import sys
(n, k, M) = map(int, sys.stdin.readline().split())
s = list(input().split())
a = list(map(int, sys.stdin.readline().split()))
m = []
for _ in range(k):
	m.append(list(map(int, sys.stdin.readline().split())))
s2 = list(input().split())
for i in range(k):
	if m[i][0] != 1:
		mina = None
		for j in range(1, len(m[i])):
			if j == 0:
				continue
			if mina == None:
				mina = a[m[i][j] - 1]
			elif a[m[i][j] - 1] < mina:
				mina = a[m[i][j] - 1]
		for j in range(0, len(m[i])):
			if j == 0:
				continue
			a[m[i][j] - 1] = mina
dt = {}
dt = {s[i]: a[i] for i in range(n)}
sm = 0
for i in range(0, M):
	sm += dt[s2[i]]
print(sm)


def init(n):
	father = [i for i in range(n)]
	return father

def find_father(x):
	if father[x] == x:
		return x
	else:
		father[x] = find_father(father[x])
		return father[x]

def merge(i, j):
	(a, b) = (find_father(i), find_father(j))
	if weights[a] <= weights[b]:
		father[b] = a
	else:
		father[a] = b
(n, k, m) = map(int, input().split())
father = init(n)
words = input().split()
words_weights = dict(zip(words, father))
weights = list(map(int, input().split()))
for _ in range(k):
	indexes = list(map(lambda x: int(x) - 1, input().split()[1:]))
	indexes_start = indexes[:-1]
	indexes_end = indexes[1:]
	for (start, end) in zip(indexes_start, indexes_end):
		merge(start, end)
secret_string = input().split()
res = [weights[find_father(words_weights[word])] for word in secret_string]
print(sum(res))


(n, k, m) = list(map(int, input().split()))
words = list(input().split())
costs = list(map(int, input().split()))
table = {}
for i in range(k):
	group = list(map(int, input().split()))[1:]
	gcosts = [costs[j - 1] for j in group]
	mcost = min(gcosts)
	for j in group:
		table[words[j - 1]] = mcost
msg = list(input().split())
msgcosts = [table[word] for word in msg]
print(sum(msgcosts))


group = [0] * 100005
(n, k, m) = map(int, input().split())
d = {}
e = 1
for i in input().split():
	d[i] = e
	e += 1
arr = list(map(int, input().split()))
arr = [0] + arr
cost = [1 << 30] * (n + 1)
for i in range(1, k + 1):
	l = list(map(int, input().split()))
	for j in range(1, l[0] + 1):
		a = l[j]
		group[a] = i
		cost[i] = min(cost[i], arr[a])
ans = 0
for i in input().split():
	ans += cost[group[d[i]]]
print(ans)


(n, k, m) = map(int, input().split())
words = input().split()
weight = list(map(int, input().split()))
flag = dict.fromkeys(words, 0)
for i in range(k):
	(t, *nums) = map(int, input().split())
	min_num = 10 ** 9 + 5
	for (j, num) in enumerate(nums):
		min_num = min(min_num, weight[num - 1])
	for (j, num) in enumerate(nums):
		flag[words[num - 1]] = min_num
words = input().split()
ans = 0
for (i, word) in enumerate(words):
	ans += flag[word]
print(ans)


(n, k, m) = map(int, input().split())
S = list(map(str, input().split()))
toid = {}
for (i, s) in enumerate(S):
	toid[s] = i
A = list(map(int, input().split()))
dmin = {}
INF = 10 ** 18
for i in range(k):
	temp = list(map(int, input().split()))
	res = INF
	for j in temp[1:]:
		res = min(res, A[j - 1])
	for j in temp[1:]:
		dmin[j - 1] = res
T = list(map(str, input().split()))
ans = 0
for t in T:
	ans += dmin[toid[t]]
print(ans)


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
from itertools import permutations
I = sys.stdin.readline

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

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def isPrime(n):
	if n <= 1:
		return False
	elif n <= 2:
		return True
	else:
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

def main():
	(n, k, m) = mi()
	words = list(I().strip().split())
	d = defaultdict(int)
	for i in range(n):
		d[words[i]] = i
	costs = li()
	for i in range(k):
		maps = li()
		tmp = math.inf
		for j in maps[1:]:
			tmp = min(tmp, costs[j - 1])
		for j in maps[1:]:
			costs[j - 1] = tmp
	msg = list(I().strip().split())
	ans = 0
	for i in msg:
		ans += costs[d[i]]
	print(ans)
main()


(n, k, m) = map(int, input().split())
st = {}
for (i, j) in enumerate(list(input().split())):
	st[j] = i
gp = [-1 for i in range(n)]
c = [int(x) for x in input().split()]
for _ in range(k):
	tl = [int(x) - 1 for x in input().split()]
	if tl[0] != 0:
		tmc = [c[x] for x in tl[1:]]
		mi = tl[1:][tmc.index(min(tmc))]
		for i in tl[1:]:
			gp[i] = mi
tc = 0
q = list(input().split())
for i in q:
	tc += c[gp[st[i]]] if gp[st[i]] != -1 else c[st[i]]
print(tc)


(n, k, m) = map(int, input().split())
M = {}
s = input().split()
for x in s:
	M[x] = 10 ** 9
a = list(map(int, input().split()))
for i in range(k):
	cin = list(map(int, input().split()))
	Size = cin[0]
	Min = 10 ** 9
	for j in range(1, Size + 1):
		Min = min(Min, a[cin[j] - 1])
	for j in range(1, Size + 1):
		M[s[cin[j] - 1]] = Min
word = input().split()
ans = 0
for y in word:
	ans += M[y]
print(ans)


(a, b, c) = map(int, input().split())
s = list(map(str, input().split()))
x = list(map(int, input().split()))
d = {}
for i in range(a):
	d[s[i]] = x[i]
for i in range(b):
	y = list(map(int, input().split()))
	l = y[1]
	for j in range(y[0]):
		d[s[l - 1]] = min(x[y[j + 1] - 1], d[s[l - 1]])
	for j in range(y[0]):
		d[s[y[j + 1] - 1]] = d[s[l - 1]]
z = list(map(str, input().split()))
sumx = 0
for i in range(len(z)):
	sumx += d[z[i]]
print(sumx)


(n, k, m) = map(int, input().split())
language_array = input().split()
cost_array = list(map(int, input().split()))
language_dic = {}
for i in range(0, n):
	language_dic[language_array[i]] = i + 1

def get_min(ids, costs):
	tmp_min = int(1000000000.0)
	for tmp_id in ids:
		tmp_min = min(tmp_min, costs[tmp_id - 1])
	return tmp_min
cost_dic = {}
for i in range(0, k):
	group = list(map(int, input().split()))
	x = group[0]
	del group[:1]
	tmp_min = get_min(group, cost_array)
	for j in range(0, x):
		cost_dic[group[j]] = tmp_min
ans = 0
for tmp_str in input().split():
	tmp_id = language_dic[tmp_str]
	ans += cost_dic[tmp_id]
print(ans)


(n, k, m) = input().split()
n = int(n)
m = int(m)
k = int(k)
s = input().split()
a = list(map(int, input().split()))
d = {}
for i in range(k):
	c = list(map(int, input().split()))
	if c[0] > 0:
		mini = a[c[1] - 1]
		for j in range(2, c[0] + 1):
			if a[c[j] - 1] < mini:
				mini = a[c[j] - 1]
		for j in range(1, c[0] + 1):
			a[c[j] - 1] = mini
for i in range(n):
	d[s[i]] = a[i]
sm = input().split()
sumi = 0
for i in range(m):
	sumi += d[sm[i]]
print(sumi)


(n, k, m) = map(int, input().split())
words = ['']
words.clear()
words.append(next(map(int, [0])))
words.extend(input().split())
a = [0]
a.extend(map(int, input().split()))
for _ in range(k):
	group = list(map(int, input().split()))[1:]
	res = min((a[y] for y in group))
	for i in group:
		a[i] = res
sd_row = {word: a[i] for (i, word) in enumerate(words)}
message = list(input().split())
print(sum((sd_row[word] for word in message)))


(n, k, m) = [int(x) for x in input().split()]
words = [str(x) for x in input().split()]
cost = [int(x) for x in input().split()]
dict = {}
for i in range(n):
	dict[words[i]] = cost[i]

def Mina(a, b):
	if a < b:
		return a
	else:
		return b
for i in range(k):
	a = [int(x) for x in input().split()]
	min_cost = 1000000000
	for j in range(1, a[0] + 1):
		min_cost = Mina(min_cost, cost[a[j] - 1])
	for j in range(1, a[0] + 1):
		cost[a[j] - 1] = min_cost
		dict[words[a[j] - 1]] = min_cost
msg = [str(x) for x in input().split()]
ans = 0
for i in range(m):
	ans = ans + dict[msg[i]]
print(ans)


d = dict()
(n, k, m) = map(int, input().split())
words = input().split()
costs = list(map(int, input().split()))
for i in range(k):
	b = list(map(int, input().split()))
	mincost = costs[b[1] - 1]
	for g in range(1, len(b)):
		if costs[b[g] - 1] < mincost:
			mincost = costs[b[g] - 1]
	for j in range(1, len(b)):
		d[words[b[j] - 1]] = mincost
messege = input().split()
total = 0
for i in range(m):
	total += d[messege[i]]
print(total)


from sys import stdin, stdout
inp = lambda : (int(o) for o in stdin.readline().split())
(n, k, m) = inp()
a = list((i for i in stdin.readline().split()))
c = list(inp())
ans = 0
x = [[] for i in range(k)]
for i in range(k):
	x = list(inp())
	cc = min((c[j - 1] for j in x[1:]))
	for i in x[1:]:
		c[i - 1] = cc
p = list((i for i in stdin.readline().split()))
b = dict(zip(a, c))
for i in p:
	ans += b[i]
print(ans)


while True:
	try:
		(n, k, m) = map(int, input().strip().split())
		str = input().strip().split()
		b = list(map(int, input().strip().split()))
		mapp = {}
		a = [9999999999] * (n + 1)
		for i in range(0, k):
			tp = list(map(int, input().strip().split()))
			for j in range(1, tp[0] + 1):
				a[i] = min(a[i], b[tp[j] - 1])
				mapp[str[tp[j] - 1]] = i
		sum = 0
		t = input().strip().split()
		for i in t:
			sum += a[mapp[i]]
		print(sum)
	except:
		break


import sys
(n, k, m) = [int(x) for x in input().split(' ')]
st = input().split(' ')
cost = {}
c = [int(x) for x in input().split(' ')]
for i in range(n):
	cost[st[i]] = c[i]
for i in range(k):
	a = [int(x) for x in input().split(' ')]
	minm = c[a[1] - 1]
	for j in range(1, a[0] + 1):
		minm = min(minm, c[a[j] - 1])
	for j in range(1, a[0] + 1):
		cost[st[a[j] - 1]] = minm
s = input().split(' ')
co = 0
for i in s:
	co += cost[i]
print(co)


def rl():
	return list(map(int, input().split()))
(n, k, m) = rl()
words = {}
wordlist = input().split()
for i in range(n):
	words[wordlist[i]] = i
costs = rl()
groups = {}
for i in range(k):
	group = rl()[1:]
	for word_id in group:
		word_id -= 1
		groups[word_id] = i
min_costs = {x: 10 ** 10 for x in range(k)}
for i in range(n):
	min_costs[groups[i]] = min(min_costs[groups[i]], costs[i])
sentence = input().split()
total_cost = 0
for word in sentence:
	word_id = words[word]
	group_id = groups[word_id]
	total_cost += min_costs[group_id]
print(total_cost)


(n, k, m) = list(map(int, input().strip().split(' ')))
words = input().strip().split()
wordsdict = {}
for (i, word) in enumerate(words):
	wordsdict[i + 1] = word
costs = list(map(int, input().strip().split(' ')))
costsdict = {}
for (i, cost) in enumerate(costs):
	costsdict[wordsdict[i + 1]] = cost
for _ in range(k):
	(x, *indices) = list(map(int, input().strip().split(' ')))
	minc = 10 ** 9
	for index in indices:
		if costs[index - 1] < minc:
			minc = costs[index - 1]
	for index in indices:
		costsdict[wordsdict[index]] = minc
sentence = input().strip().split()
ans = 0
for word in sentence:
	ans += costsdict[word]
print(ans)


(n, k, m) = [int(x) for x in input().split()]
language = input().split()
costs = [int(x) for x in input().split()]
d = {}
for i in range(k):
	a = [int(x) for x in input().split()]
	(x, a) = (a[0], a[1:])
	mincost = min(map(lambda e: costs[e - 1], a))
	for e in a:
		costs[e - 1] = mincost
d = {word: cost for (word, cost) in zip(language, costs)}
sentence = input().split()
sumcost = sum(map(lambda word: d[word], sentence))
print(sumcost)


(n, k, m) = map(int, input().split())
word = input().split()
cost = list(map(int, input().split()))
for i in range(k):
	arr = list(map(int, input().split()))
	m = cost[arr[1] - 1]
	for j in range(1, len(arr)):
		if cost[arr[j] - 1] < m:
			m = cost[arr[j] - 1]
	for j in range(1, len(arr)):
		cost[arr[j] - 1] = m
d = {word[i]: cost[i] for i in range(len(word))}
message = input().split()
price = 0
for i in message:
	price = price + d[i]
print(price)


def test():
	(a, b, m) = map(int, input().split())
	words = list(input().split())
	costs = list(map(int, input().split()))
	dicts = dict(zip(words, costs))
	for i in range(0, b):
		tmp = list(map(int, input().split()))
		length = tmp.__len__()
		if length > 2:
			mins = 0
			for j in range(1, length - 1):
				tmpMin = min(costs[tmp[j] - 1], costs[tmp[j + 1] - 1])
				if j == 1:
					mins = tmpMin
				mins = min(mins, tmpMin)
			for j in range(1, length):
				dicts[words[tmp[j] - 1]] = mins
	target = list(input().split())
	res = 0
	for i in range(0, target.__len__()):
		res += dicts[target[i]]
	print(res)
test()


import sys
rl = lambda : sys.stdin.readline()
(n, k, m) = tuple(map(int, rl().split()))
words = rl().rstrip().split()
costs = [int(cost) for cost in rl().rstrip().split()]
for _ in range(k):
	group = [int(num) for num in rl().rstrip().split()]
	if group[0] == 0:
		continue
	else:
		min = 1000000001
		for i in range(group[0]):
			if costs[group[i + 1] - 1] < min:
				min = costs[group[i + 1] - 1]
		for i in range(group[0]):
			costs[group[i + 1] - 1] = min
word_dict = {}
for i in range(n):
	word_dict[words[i]] = costs[i]
final_str = rl().rstrip().split()
sum = 0
for word in final_str:
	sum += word_dict[word]
print(sum)


import sys
import os.path
from collections import *
import math
import bisect
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = sys.stdin.readline
(n, k, m) = [int(x) for x in input().split()]
h1 = Counter()
s = input().split()
arr = [int(x) for x in input().split()]
h = Counter()
for i in range(n):
	h[s[i]] = arr[i]
for i in range(k):
	brr = [int(x) for x in input().split()]
	minval = arr[brr[1] - 1]
	for i in range(1, brr[0] + 1):
		minval = min(minval, arr[brr[i] - 1])
	for i in range(1, brr[0] + 1):
		h[s[brr[i] - 1]] = minval
cost = 0
s1 = input().split()
for i in range(m):
	cost += h[s1[i]]
print(cost)


var = [int(x) for x in input().split()]
n = var[0]
k = var[1]
m = var[2]
words = input().split()
m = {}
for i in range(n):
	m[words[i]] = i
c = {}
cost = [int(x) for x in input().split()]
for i in range(k):
	y = [int(x) - 1 for x in input().split()][1:n + 1]
	minc = min([cost[x] for x in y])
	for x in y:
		c[x] = minc
print(sum([c[m[x]] for x in input().split()]))


(n, k, m) = map(int, input().split())
s = input().split()
a = list(map(int, input().split()))
b = [0] * (n + 1)
a = [0] + a
for i in range(n):
	b[i] = a[i]
M = dict()
for i in range(k):
	d = list(map(int, input().split()))
	x = a[d[1]]
	for j in d[1:]:
		x = min(x, a[j])
	for j in d[1:]:
		b[j] = x
for i in range(n):
	M[s[i]] = b[i + 1]
t = input().split()
ans = 0
for i in range(m):
	ans += M[t[i]]
print(ans)


(n, k, m) = map(int, input().split())
v = input().split()
c = list(map(int, input().split()))
for _ in range(k):
	w = list(map(int, input().split()))
	if w[0] == 1:
		continue
	vale = [c[x - 1] for x in w[1:]]
	m = vale.index(min(vale))
	min_value = c[w[m + 1] - 1]
	for c1 in w[1:]:
		c[c1 - 1] = min_value
d = dict(zip(v, c))
t = input().split()
c2 = sum([d[x] for x in t])
print(c2)


(n, k, m) = [int(x) for x in input().split()]
s = input().split()
a = [int(x) for x in input().split()]
kd = [[int(x) for x in input().split()] for _ in range(k)]
trgt = input().split()
costs = {}
for group in kd:
	g2 = group[1:]
	cost = min((a[x - 1] for x in g2))
	for x in g2:
		costs[x] = cost
wmap = {word: i + 1 for (i, word) in enumerate(s)}
ans = 0
for w in trgt:
	ans += costs[wmap[w]]
print(ans)


(n, k, m) = map(int, input().split())
w = input().split()
a = list(map(int, input().split()))
groupdict = {}
groupmin = []
for __ in range(k):
	s = list(map(int, input().split()))
	mn = 10 ** 10
	for i in range(1, len(s)):
		groupdict[w[s[i] - 1]] = __
		if a[s[i] - 1] < mn:
			mn = a[s[i] - 1]
	groupmin.append(mn)
msg = input().split()
cost = 0
for word in msg:
	cost += groupmin[groupdict[word]]
print(cost)


(n, k, m) = map(int, input().split())
slova = list(input().split())
stoim = list(map(int, input().split()))
a = {}
for i in range(k):
	s = list(map(int, input().split()))
	h = int(s[0])
	s.pop(0)
	st = []
	for j in range(h):
		st.append(stoim[s[j] - 1])
	l = min(st)
	for _ in range(h):
		a[slova[s[_] - 1]] = l
mes = list(input().split())
q = 0
for i in range(len(mes)):
	q += a[mes[i]]
print(q)


(n, k, m) = map(int, input().split())
slova = input().split()
cost = input().split()
d = {}
slgr = {}
for i in range(len(slova)):
	d[slova[i]] = int(cost[i])
gr = [0] * k
for i in range(k):
	s = input().split()
	gr[i] = []
	for j in range(1, len(s)):
		c = int(s[j]) - 1
		gr[i].append(slova[c])
		slgr[slova[c]] = i
	if len(gr[i]) > 1:
		tmpi = gr[i][0]
		mini = d[gr[i][0]]
		for j in gr[i]:
			if mini > d[j]:
				tmpi = j
				mini = d[j]
		gr[i] = tmpi
	else:
		gr[i] = gr[i][0]
sms = input().split()
for i in range(len(sms)):
	sms[i] = gr[slgr[sms[i]]]
count = 0
for i in sms:
	count += d[i]
print(count)


(n, k, m) = (int(x) for x in input().split())
lang = [str(x) for x in input().split()]
cost = [int(x) for x in input().split()]
groups = dict()
min_for_group = []
for i in range(k):
	min_ = 10 ** 9 + 50
	arr = [int(x) for x in input().split()]
	count = arr[0]
	arr = arr[1:]
	for j in range(count):
		groups[lang[arr[j] - 1]] = i
		if cost[arr[j] - 1] < min_:
			min_ = cost[arr[j] - 1]
	min_for_group.append(min_)
s = input().split()
min_cost = 0
for i in s:
	min_cost += min_for_group[groups[i.lower()]]
print(min_cost)


nkm = input().split(' ')
(n, k, m) = (int(nkm[0]), int(nkm[1]), int(nkm[2]))
words = input().split(' ')
prices = input().split(' ')
price = list()
max = 0
for p in prices:
	if int(p) > max:
		max = int(p)
	price.append(int(p))
wordsin = list()
for i in range(k):
	tn = list()
	t = input().split(' ')
	t.pop(0)
	for ti in t:
		tn.append(int(ti))
	wordsin.append(tn)
word = input().split(' ')
wordict = dict()
for l in wordsin:
	min = max
	for s in l:
		if price[s - 1] < min:
			min = price[s - 1]
	for s in l:
		wordict[words[s - 1]] = min
finprice = 0
for w in word:
	finprice += wordict[w]
print(finprice)


(n, k, m) = map(int, input().split())
l = input().split()
c = list(map(int, input().split()))
d = dict(zip(l, c))
simel = []
for i in range(k):
	siml = list(map(int, input().split()))
	mini = []
	for j in range(siml[0]):
		mini.append(d[l[siml[j + 1] - 1]])
	mini = min(mini)
	for k in range(siml[0]):
		d[l[siml[k + 1] - 1]] = mini
	simel.append(siml)
rl = input().split()
value = 0
for i in rl:
	value += d[i]
print(value)


(n, k, m) = list(map(int, input().split()))
d = list(map(str, input().split()))
dic = dict()
co = list(map(int, input().split()))
for i in range(k):
	l = list(map(int, input().split()))
	p = []
	for i in range(1, l[0] + 1):
		p += [[co[l[i] - 1], d[l[i] - 1]]]
	u = min(p)
	for i in range(1, l[0] + 1):
		dic[d[l[i] - 1]] = u
poi = list(map(str, input().split()))
cost = 0
for i in range(m):
	cost += dic[poi[i]][0]
print(cost)


(n, k, m) = map(int, input().split())
string_repo = input().split()
string_weights = list(map(int, input().split()))
synonymous_indexes = []
for _ in range(k):
	synonymous_indexes.append(list(map(int, input().split()[1:])))
secret_string = input().split()
for indexes in synonymous_indexes:
	min_weight = min([string_weights[i - 1] for i in indexes])
	for i in indexes:
		string_weights[i - 1] = min_weight
str_dict = dict(zip(string_repo, string_weights))
res = [str_dict[s] for s in secret_string]
print(sum(res))


(n, k, m) = [int(x) for x in input().split(' ')]
words = [x for x in input().split(' ')]
cost = [int(x) for x in input().split(' ')]
d = {}
for i in range(k):
	temp = [int(x) - 1 for x in input().split(' ')]
	temp.pop(0)
	m = 1000000000
	e = min(list(map(lambda x: cost[x], temp)))
	for w in temp:
		d[words[w]] = e
ans = 0
msg = [d[x] for x in input().split(' ')]
print(sum(msg))


(n, ki, m) = map(int, input().split())
k = input().split()
l = list(map(int, input().split()))
ind = range(1, n + 1)
d1 = dict(zip(k, l))
d2 = {}
for _ in range(ki):
	d = list(map(int, input().split()))
	if len(d) > 2:
		f = []
		for i in d[1:]:
			f.append([k[i - 1], l[i - 1]])
		f.sort(key=lambda x: x[-1])
		for i in f[1:]:
			d2[i[0]] = f[0][0]
	else:
		d2[k[d[1] - 1]] = k[d[1] - 1]
send = input().split()
cost = 0
for i in send:
	try:
		cost += d1[d2[i]]
	except:
		cost += d1[i]
print(cost)


def find_min_cost_word(words, group_words, costs):
	cost_min = 10000000000
	for word_ind in group_words:
		cost_word = costs[word_ind - 1]
		cost_min = min(cost_min, cost_word)
	return cost_min
(n, k, m) = list(map(int, input().split()))
words = input().split()
costs = list(map(int, input().split()))
groups = []
d = {}
for i in range(k):
	tmp = list(map(int, input().split()))
	(x, group_words) = (tmp[0], tmp[1:])
	min_cost_word = find_min_cost_word(words, group_words, costs)
	for word_ind in group_words:
		word = words[word_ind - 1]
		d[word] = min_cost_word
message = input().split()
c = 0
for w in message:
	c += d[w]
print(c)


inp_rot = 0
inp_arr = []

def inp_reload():
	global inp_rot
	global inp_arr
	inp_rot = 0
	inp_arr = input().split(' ')
	return

def inp():
	global inp_arr
	global inp_rot
	ret = inp_arr[inp_rot]
	inp_rot += 1
	return ret
inp_reload()
n = int(inp())
k = int(inp())
m = int(inp())
a = {}
id_list = [0]
wrd_idx = {}
inp_reload()
for i in range(n):
	w = inp()
	a[w] = 0
	id_list.append([w])
	wrd_idx[w] = i + 1
inp_reload()
for key in a:
	a[key] = int(inp())
g = []
for i in range(k):
	g.append(0)
	inp_reload()
	for j in range(int(inp())):
		idn = int(inp())
		id_list[idn].append(i)
		if a[id_list[idn][0]] < g[i] or g[i] == 0:
			g[i] = a[id_list[idn][0]]
suma = 0
inp_reload()
for i in range(m):
	w = inp()
	suma += g[id_list[wrd_idx[w]][1]]
print(suma)


from collections import deque as de
import math
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping
from itertools import groupby as gb

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))
(n, k, m) = map(int, input().split())
wordindex = {}
for (i, v) in enumerate(input().split()):
	wordindex[int(i) + 1] = v
costindex = {}
for (i, v) in enumerate(input().split()):
	costindex[int(i) + 1] = int(v)
grpcostdict = {}
wordgrp = {}
kk = k
while k:
	k -= 1
	temp = list(map(int, input().split()))
	grpcostlist = []
	for i in range(1, temp[0] + 1):
		grpcostlist.append(costindex[temp[i]])
		wordgrp[wordindex[temp[i]]] = kk - k
	grpcostlist.sort()
	grpcostdict[kk - k] = grpcostlist
message = list(map(str, input().split()))
ans = 0
for i in range(m):
	ans += grpcostdict[wordgrp[message[i]]][0]
print(ans)


(n, k, m) = map(int, input().split())
word = list(input().split())
cost = list(map(int, input().split()))
maps = {}
for j in range(k):
	l = list(map(int, input().split()))
	minimum = cost[l[1] - 1]
	for k in range(2, len(l)):
		if cost[l[k] - 1] < minimum:
			minimum = cost[l[k] - 1]
	for k in range(1, len(l)):
		maps[word[l[k] - 1]] = minimum
query = list(input().split())
total = 0
for i in query:
	total += maps[i]
print(total)


(n, k, m) = [int(x) for x in input().split()]
words = input().split()
(c, d) = ([int(x) for x in input().split()], {})
for i in range(k):
	ii = list(map(int, input().split()[1:]))
	mn = float('inf')
	for j in ii:
		mn = min(mn, c[j - 1])
	for j in ii:
		d[words[j - 1]] = mn
ans = 0
for i in input().split():
	ans += d[i]
print(ans)


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

def mi():
	return map(int, input().split())

def li():
	return list(mi())
(n, k, m) = mi()
s = [''] + input().split()
a = [0] + li()
g = [0] * (k + 1)
p = {}
for i in range(1, k + 1):
	b = li()[1:]
	g[i] = min((a[x] for x in b))
	for x in b:
		p[s[x]] = i
msg = input().split()
ans = sum((g[p[w]] for w in msg))
print(ans)


line = [int(e) for e in input().split(' ')]
n = line[0]
k = line[1]
line = input().split(' ')
line2 = [int(e) for e in input().split(' ')]
score = {}
for j in range(n):
	score[line[j]] = line2[j]
for j in range(k):
	line2 = [int(e) - 1 for e in input().split(' ')]
	line2.remove(line2[0])
	minscore = score[line[line2[0]]]
	for e in line2:
		minscore = min(minscore, score[line[e]])
	for e in line2:
		score[line[e]] = minscore
print(sum([score[e] for e in input().split(' ')]))


from collections import defaultdict as dd
(n, k, m) = [int(i) for i in input().split()]
a = [str(i) for i in input().split()]
cost = [int(i) for i in input().split()]
dic1 = dd(int)
for I in range(1, k + 1):
	b = [int(i) for i in input().split()]
	min1 = 10 ** 9 + 9
	for i in range(1, len(b)):
		min1 = min(min1, cost[b[i] - 1])
	for i in range(1, len(b)):
		dic1[a[b[i] - 1]] = min1
words = [str(i) for i in input().split()]
tot = 0
for i in words:
	tot += dic1[i]
print(tot)


(n, k, m) = map(int, input().split())
s = list(input().split())
l = list(map(int, input().split()))
for i in range(k):
	a = list(map(int, input().split()))
	n1 = a[0]
	a = a[1:]
	mini = 1000000001
	for j in a:
		if l[j - 1] < mini:
			mini = l[j - 1]
	for j in a:
		l[j - 1] = mini
d = {}
d = {s[i]: l[i] for i in range(n)}
m1 = list(input().split())
su = 0
for i in m1:
	su = su + d[i]
print(su)


dict = {}
(n, m, k) = map(int, input().split())
sum = 0
a = list(map(str, input().split()))
for i in range(n):
	dict[a[i]] = i
b = list(map(int, input().split()))
for i in range(m):
	c = list(map(int, input().split()))
	r = 10 ** 9
	for i in c[1:]:
		r = min(r, b[i - 1])
	for i in c[1:]:
		b[i - 1] = r
d = list(map(str, input().split()))
for i in d:
	sum += b[dict[i]]
print(sum)


def main():
	(n, k, m) = map(int, input().split())
	words = list(input().split())
	costs = list(map(int, input().split()))
	dicts = dict(zip(words, costs))
	for i in range(0, k):
		tmp = list(map(int, input().split()))
		length = tmp.__len__()
		if length > 2:
			mins = 0
			for j in range(1, length - 1):
				tmpMin = min(costs[tmp[j] - 1], costs[tmp[j + 1] - 1])
				if j == 1:
					mins = tmpMin
				mins = min(mins, tmpMin)
			for j in range(1, length):
				dicts[words[tmp[j] - 1]] = mins
	target = list(input().split())
	res = 0
	for i in range(0, target.__len__()):
		res += dicts[target[i]]
	print(res)
main()


(n, k, m) = map(int, input().split())
a = list(map(str, input().split()))
l = list(map(int, input().split()))
dict = {}
for i in range(k):
	lst = list(input().split())
	c = list(map(int, lst[1:]))
	mini = min([l[i - 1] for i in c])
	for i in c:
		l[i - 1] = mini
for i in range(n):
	dict[a[i]] = l[i]
r = list(map(str, input().split()))
ans = 0
for i in range(m):
	ans += dict[r[i]]
print(ans)


import sys
import math
d = dict()
(n, k, m) = map(int, input().split())
s = input().split()
b = [0] * (n + 5)
a = list(map(int, input().split()))
for i in range(k):
	c = list(map(int, input().split()))
	for j in range(1, len(c)):
		d[s[c[j] - 1]] = i + 1
		b[c[j]] = i + 1
m = [10 ** 10] * (k + 4)
for i in range(1, n + 1):
	if a[i - 1] < m[b[i]]:
		m[b[i]] = a[i - 1]
ss = input().split()
sum1 = 0
for i in range(len(ss)):
	cl = d[ss[i]]
	sum1 += m[cl]
print(sum1)


(n, k, m) = map(int, input().split())
s = list(input().split(' '))
a = list(map(int, input().split()))
INF = float('inf')
dic = {}
for i in range(k):
	b = list(map(int, input().split()))
	b = b[1:]
	rec = INF
	for j in b:
		rec = min(rec, a[j - 1])
	for j in b:
		dic[s[j - 1]] = rec
t = list(input().split())
num = 0
for q in t:
	num += dic[q]
print(num)


(n, k, m) = map(int, input().split())
palabras = {x: y + 1 for (y, x) in enumerate(input().split())}
costos = [int(x) for x in input().split()]
traspasos = [x for x in range(n + 1)]
costos_minimos = costos[:]
for e in range(k):
	lista = [int(x) for x in input().split()]
	lista.pop(0)
	m = 10 ** 10
	for q in lista:
		if costos[q - 1] < m:
			m = costos[q - 1]
	for q in lista:
		costos_minimos[q - 1] = m
mensaje = input().split()
suma = 0
for e in mensaje:
	suma += costos_minimos[palabras[e] - 1]
print(suma)


a = input().split(' ')
n = int(a[0])
k = int(a[1])
m = int(a[2])
a = input().split(' ')
idx = {}
stt = {}
ref = {}
cnt = 1
for i in a:
	stt[cnt] = i
	idx[i] = cnt
	ref[i] = i
	cnt += 1
a = input().split(' ')
cost = {}
cnt = 1
for i in a:
	cost[cnt] = int(i)
	cnt += 1
for i in range(k):
	a = input().split(' ')
	a = a[1:]
	a = [int(j) for j in a]
	b = a
	a = [cost[j] for j in a]
	mini = min(a)
	for j in b:
		ref[j] = mini
ans = 0
a = input().split(' ')
for i in a:
	ans += ref[idx[i]]
print(ans)


(n, k, m) = map(int, input().split())
ws = list(input().split())
ns = list(map(int, input().split()))
ks = list((list(map(lambda x: int(x) - 1, input().split()[1:])) for i in range(k)))
s = list(input().split())
ws1 = dict()
o = 0
mi0 = max(ns)
for i in range(k):
	(ind, mi) = (0, mi0)
	for j in range(len(ks[i])):
		if ns[ks[i][j]] < mi:
			(ind, mi) = (j, ns[ks[i][j]])
	for j in ks[i]:
		ws1[ws[j]] = ns[ks[i][ind]]
for i in s:
	o += ws1[i]
print(o)


(n, k, m) = [int(x) for x in input().split()]
me = input().split()
a = [int(x) for x in input().split()]
d = {}
g = 1
for i in range(k):
	t = [int(x) for x in input().split()]
	for j in range(1, len(t)):
		d[me[t[j] - 1]] = g
	g += 1
g = 0
fin = {}
for i in range(1, k + 1):
	fin[i] = 1000000000
for i in range(n):
	fin[d[me[i]]] = min(a[i], fin[d[me[i]]])
ans = 0
l = input().split()
for i in l:
	ans += fin[d[i]]
print(ans)


from copy import deepcopy

class DSU:

	def __init__(self, n, cc):
		self.n = n
		self.par = list(range(self.n))
		self.size = [1 for i in range(self.n)]
		self.cost = cc

	def find(self, i):
		while i != self.par[i]:
			self.par[i] = self.par[self.par[i]]
			i = self.par[i]
		return i

	def unite(self, a, b):
		par_a = self.find(a)
		par_b = self.find(b)
		if par_a == par_b:
			return False
		else:
			if self.cost[par_a - 1] <= self.cost[par_b - 1]:
				self.par[par_b] = par_a
				self.size[par_a] += self.size[par_b]
				self.cost[par_b - 1] = self.cost[par_a - 1]
			else:
				self.par[par_a] = par_b
				self.size[par_b] += self.size[par_a]
				self.cost[par_a - 1] = self.cost[par_b - 1]
			return True

	def same(self, a, b):
		self.find(a)
		self.find(b)
		return self.par[a] == self.par[b]

	def size(self, a):
		return self.size[a]

def solve(words, d, r, cost, message, dsu, cc):
	n = len(words)
	minn = deepcopy(cc)
	minCost = 0
	for word in message:
		parA = dsu.find(d[word])
		minCost += dsu.cost[parA - 1]
	return minCost
(n, k, m) = list(map(int, input().split()))
words = list(map(str, input().split()))
cc = list(map(int, input().split()))
dsu = DSU(n + 1, cc)
(r, d) = ({}, {})
cost = {}
for i in range(n):
	cost[i + 1] = cc[i]
for i in range(n):
	d[words[i]] = i + 1
for i in d:
	r[d[i]] = i
for kk in range(k):
	ll = list(map(int, input().split()))
	if ll[0] != 1:
		for i in range(1, ll[0]):
			dsu.unite(ll[i], ll[i + 1])
message = list(map(str, input().split()))
print(solve(words, d, r, cost, message, dsu, cc))


(n, k, m) = map(int, input().split())
words = {}
x = input().split()
y = [*map(int, input().split())]
for (i, j) in zip(x, y):
	words[i] = j
for _ in range(k):
	t = [*map(int, input().split())]
	tt = [words[x[i - 1]] for i in t[1:]]
	ttt = min(tt)
	for i in t[1:]:
		words[x[i - 1]] = ttt
print(sum((words[i] for i in input().split())))


(n, k, m) = map(int, input().split())
arr = list(map(str, input().split()))
d = dict()
for i in range(n):
	d[arr[i]] = i
coin = list(map(int, input().split()))
coin2 = [0 for i in range(n)]
for i in range(k):
	mass = list(map(int, input().split()))
	mass2 = []
	for j in range(1, len(mass)):
		mass2.append(mass[j] - 1)
	mi = coin[mass2[0]]
	for j in range(len(mass2)):
		if mi > coin[mass2[j]]:
			mi = coin[mass2[j]]
	for j in range(len(mass2)):
		coin2[mass2[j]] = mi
arr2 = list(map(str, input().split()))
ans = 0
for i in range(m):
	ind = d[arr2[i]]
	ans += coin2[ind]
print(ans)


(n, k, m) = map(int, input().split())
word = input().split()
cost = list(map(int, input().split()))
cost_dic = dict(zip(word, cost))
for i in range(k):
	index = list(map(int, input().split()))
	index = index[1:]
	z = cost_dic[word[index[0] - 1]]
	for j in index:
		j -= 1
		t = cost_dic[word[j]]
		z = min(z, t)
	for j in index:
		j -= 1
		cost_dic[word[j]] = z
ans = 0
for i in input().split():
	ans += cost_dic[i]
print(ans)


(n, k, m) = map(int, input().strip().split())
lex = input().strip().split()
a = [int(x) for x in input().strip().split()]
for j in range(k):
	args = [int(x) - 1 for x in input().strip().split()]
	args.pop(0)
	min_a = min((a[i] for i in args))
	for i in args:
		a[i] = min_a
prices = dict(zip(lex, a))
sentence = input().strip().split()
total = 0
for word in sentence:
	total += prices[word]
print(total)


line = input().split(' ')
numLang = int(line[0])
numGroups = int(line[1])
numMessage = int(line[2])
wordsList = input().split(' ')
wordsPrice = input().split(' ')
for i in range(numLang):
	wordsPrice[i] = int(wordsPrice[i])
words = {}
for i in range(numLang):
	words[wordsList[i]] = wordsPrice[i]
for i in range(numGroups):
	line = input().split(' ')
	for j in range(len(line)):
		line[j] = int(line[j])
	if line[0] > 1:
		minimum = -1
		for j in range(1, len(line)):
			word = wordsList[line[j] - 1]
			if minimum == -1 or minimum > words[word]:
				minimum = words[word]
		for j in range(1, len(line)):
			word = wordsList[line[j] - 1]
			words[word] = minimum
line = input().split(' ')
sum = 0
for i in range(numMessage):
	sum = sum + words[line[i]]
print(sum)


class DSU:

	def __init__(self, n):
		self.n = n
		self.comps = n
		self.parent = [i for i in range(n)]

	def FIND(self, x):
		if x != self.parent[x]:
			self.parent[x] = self.FIND(self.parent[x])
		return self.parent[x]

	def UNION(self, x, y):
		xRoot = self.FIND(x)
		yRoot = self.FIND(y)
		if xRoot != yRoot:
			self.comps -= 1
			if costs[xRoot] <= costs[yRoot]:
				self.parent[yRoot] = xRoot
			else:
				self.parent[xRoot] = yRoot
			return 1
		return 0
(n, k, m) = map(int, input().split())
words = {val: ind for (ind, val) in enumerate(input().split())}
costs = [int(x) for x in input().split()]
dsu = DSU(n)
ans = 0
while k:
	arr = [int(x) - 1 for x in input().split()][1:]
	first = arr[0]
	for item in arr[1:]:
		dsu.UNION(first, item)
	k -= 1
sentence = input().split()
for word in sentence:
	ind = words[word]
	ans += costs[dsu.FIND(ind)]
print(ans)


(n, k, m) = list(map(int, input().split()))
arr = list(input().split())
cost = list(map(int, input().split()))
dict1 = dict()
cost2 = []
for i in range(0, k):
	arr2 = list(map(int, input().split()))
	cost3 = []
	for j in range(1, len(arr2)):
		dict1[arr[arr2[j] - 1]] = i
		cost3.append(cost[arr2[j] - 1])
	cost2.append(min(cost3))
arr4 = list(input().split())
sum = 0
for i in range(0, len(arr4)):
	sum = sum + cost2[dict1[arr4[i]]]
print(sum)


(n, k, m) = map(int, input().split())
a = input().split()
group = [0] * n
pos = dict()
for i in range(0, n):
	pos[a[i]] = i
	group[i] = i
group_cost = list(map(int, input().split()))
for i in range(k):
	ids = list(map(int, input().split()))
	size = ids[0]
	cur_group = ids[1] - 1
	ids = ids[2:]
	for j in range(size - 1):
		cur = ids[j] - 1
		group[cur] = cur_group
		group_cost[cur_group] = min(group_cost[cur_group], group_cost[cur])
ans = 0
strs = input().split()
for i in range(m):
	s = strs[i]
	ans += group_cost[group[pos[s]]]
print(ans)


(n, k, m) = map(int, input().split())
words = list(input().split())
costs = list(map(int, input().split()))
(dic, dic1) = ({}, {})
for i in range(k):
	a = list(map(int, input().split()))
	(p, q) = (a[0], a[1:])
	minc = float('inf')
	for j in a[1:]:
		dic[j] = i
		minc = min(minc, costs[j - 1])
	for j in a[1:]:
		dic1[words[j - 1]] = minc
message = list(input().split())
cost = 0
for j in message:
	cost += dic1[j]
print(cost)


import sys
import math
(n, k, m) = list(map(int, sys.stdin.readline().strip().split(' ')))
word_indexes = {word: i for (i, word) in enumerate(sys.stdin.readline().strip().split(' '))}
costs = list(map(int, sys.stdin.readline().strip().split(' ')))
min_costs = costs[:]
for ki in range(k):
	group = list(map(int, sys.stdin.readline().strip().split(' ')))
	min_so_far = float('inf')
	for i in group[1:]:
		min_so_far = min(min_so_far, costs[i - 1])
	for i in group[1:]:
		min_costs[i - 1] = min_so_far
sentence = sys.stdin.readline().strip().split(' ')
res = 0
for word in sentence:
	word_index = word_indexes[word]
	res += min_costs[word_index]
print(res)


from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys

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

def iinput():
	return int(input())

def tinput():
	return input().split()

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def root(parent, a):
	while parent[a] != a:
		parent[a] = parent[parent[a]]
		a = parent[a]
	return a

def union(parent, minimum, a, b):
	(root_a, root_b) = (root(parent, a), root(parent, b))
	if root_a == root_b:
		return
	parent[root_a] = root_b
	minimum[root_b] = min(minimum[root_a], minimum[root_b])

def solve(adj, colors, n):
	pass
(n, k, m) = rinput()
words = input().split()
cost = rlinput()
word_cost = {i: j for (i, j) in zip(words, cost)}
index = {words[i]: i for i in range(n)}
minimum = defaultdict()
d = defaultdict()
for i in range(k):
	a = rlinput()
	mn = 99999999999
	for j in range(1, a[0] + 1):
		mn = min(mn, word_cost[words[a[j] - 1]])
		d[a[j] - 1] = i
	minimum[i] = mn
s = input().split()
ans = 0
for i in s:
	ans += minimum[d[index[i]]]
print(ans)


from sys import stdin

def iin():
	return int(stdin.readline())

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

def main():
	(n, k, m) = lin()
	words = input().split()
	a = {w: i for (i, w) in enumerate(words)}
	w = lin()
	group = {}
	a1 = {}
	for _ in range(k):
		su = lin()
		mn = -1
		for j in su[1:]:
			a1[j - 1] = _
			if mn == -1:
				mn = w[j - 1]
			else:
				mn = min(mn, w[j - 1])
		group[_] = mn
	s = input().split()
	ans = 0
	for i in s:
		ans += group[a1[a[i]]]
	print(ans)
main()


(n, num_group, m) = [int(c) for c in input().split(' ')]
words = input().split(' ')
word_and_id = {}
for word in words:
	word_and_id[word] = len(word_and_id)
cost = [int(c) for c in input().split(' ')]
groups = []
group_for_word = [None for i in range(n)]
for i in range(num_group):
	groups.append([int(c) - 1 for c in input().split(' ')][1:])
	for word_id in groups[-1]:
		group_for_word[word_id] = i
mincosts = [2 * 10 ** 9 for i in range(num_group)]
for i in range(num_group):
	for word_id in groups[i]:
		if mincosts[i] > cost[word_id]:
			mincosts[i] = cost[word_id]
message = input().split(' ')
answer = 0
for word in message:
	group_id = group_for_word[word_and_id[word]]
	answer += mincosts[group_id]
print(answer)


def f(x):
	return int(x) - 1
(n, k, m) = map(int, input().split())
src = input().split()
cs = list(map(int, input().split()))
for _ in range(k):
	arr = list(map(f, input().split()))[1:]
	arr.sort(key=lambda x: cs[x])
	for c in arr[1:]:
		cs[c] = cs[arr[0]]
d = {x: y for (x, y) in zip(src, cs)}
print(sum((d[x] for x in input().split())))


import sys
(n, k, m) = list(map(int, sys.stdin.readline().strip().split()))
words = [''] + sys.stdin.readline().strip().split()
costs = [0] + list(map(int, sys.stdin.readline().strip().split()))
group_to_min = [10000000000] * k
word_to_group = dict()
for i in range(n):
	word_to_group[words[i + 1]] = 0
for g in range(k):
	inpt = list(map(int, sys.stdin.readline().strip().split()))
	(_, elems) = (inpt[0], inpt[1:])
	for e in elems:
		word_to_group[words[e]] = g
		group_to_min[g] = min(group_to_min[g], costs[e])
msg = sys.stdin.readline().strip().split()
mcost = 0
for m in msg:
	mcost += group_to_min[word_to_group[m]]
print(mcost)


(n, k, m) = list(map(int, input().split()))
wordByIndex = list(input().strip().split())
indexByWord = {wordByIndex[i]: i for i in range(n)}
price = list(map(int, input().split()))
for __ in range(k):
	synonyms = set(list(map(lambda x: int(x) - 1, input().split()))[1:])
	minPrice = float('inf')
	for i in synonyms:
		if price[i] < minPrice:
			minPrice = price[i]
	for i in synonyms:
		price[i] = minPrice
out = 0
for word in list(input().strip().split()):
	out += price[indexByWord[word]]
print(out)


(n, k, m) = map(int, input().split())
words = input().split()
costs = list(map(int, input().split()))
word_to_cost = dict()
for i in range(k):
	tmp = list(map(int, input().split()))
	x = tmp[0]
	min_cost = costs[tmp[1] - 1]
	for j in range(1, x + 1):
		min_cost = min(min_cost, costs[tmp[j] - 1])
	for j in range(1, x + 1):
		word_to_cost[words[tmp[j] - 1]] = min_cost
msgs = input().split()
total_cost = 0
for word in msgs:
	total_cost += word_to_cost[word]
print(total_cost)


from collections import defaultdict

class DisjSet:

	def __init__(self):
		self.rank = defaultdict(lambda : 1)
		self.parent = defaultdict(lambda : -1)

	def find(self, x):
		if self.parent[x] == -1 or self.parent[x] == x:
			self.parent[x] = x
		else:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def Union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1
		self.parent[y] = x
(n, k, m) = map(int, input().split())
arr = list(input().split())
cost = list(map(int, input().split()))
obj = DisjSet()
cnt = defaultdict(lambda : None)
for _ in range(k):
	temp = list(map(int, input().split()))
	for val in temp[1:]:
		obj.Union(arr[temp[1] - 1], arr[val - 1])
for x in range(n):
	if cnt[obj.find(arr[x])] == None:
		cnt[obj.find(arr[x])] = cost[x]
	else:
		cnt[obj.find(arr[x])] = min(cost[x], cnt[obj.find(arr[x])])
ans = 0
arr = list(input().split())
for x in range(m):
	ans += cnt[obj.find(arr[x])]
print(ans)


from collections import defaultdict
d = defaultdict(list)
arr = list(map(int, input().split()))
n = arr[0]
k = arr[1]
m = arr[2]
s = input().split()
for i in range(n):
	d[s[i]] = i
price = list(map(int, input().split()))
for i in range(k):
	a = list(map(int, input().split()))
	p = a[0]
	x = 1000000000000
	for j in range(p):
		x = min(x, price[a[j + 1] - 1])
	for j in range(p):
		price[a[j + 1] - 1] = x
ss = input().split()
ans = 0
for i in range(m):
	ans += price[d[ss[i]]]
print(ans)


(n, k, m) = [int(x) for x in input().strip().split()]
words = input().strip().split()
wd = {}
wi = 1
for w in words:
	wd[w] = wi
	wi += 1
a = [int(x) for x in input().strip().split()]
group = {}
for i in range(k):
	grp = [int(x) for x in input().strip().split()][1:]
	mincost = a[grp[0] - 1]
	for g in grp:
		mincost = min(mincost, a[g - 1])
	for g in grp:
		group[g] = mincost
inp = input().strip().split()
cost = 0
for w in inp:
	idx = wd[w]
	cost += group[idx]
print(cost)


(n, k, m) = map(int, input().split())
arr = list(input().strip().split())
cost = list(map(int, input().split()))
same_meanings = []
for i in range(k):
	same_meanings.append(list(map(int, input().split()))[1:])
s = list(input().strip().split())
groups = {}
for i in range(k):
	for j in same_meanings[i]:
		groups[j] = i
minn_arr = []
for i in range(k):
	minn = 10 ** 20
	for j in same_meanings[i]:
		if cost[j - 1] < minn:
			minn = cost[j - 1]
	minn_arr.append(minn)
index = {}
for i in range(len(arr)):
	index[arr[i]] = i
ans = 0
for i in s:
	ans += minn_arr[groups[index[i] + 1]]
print(ans)


(n, k, m) = map(int, list(input().strip().split()))
w = list(input().split())
c = dict()
for (i, j) in enumerate(map(int, list(input().split()))):
	c[w[i]] = j
for i in range(k):
	l = [int(z) for z in list(input().split())]
	mini = float('inf')
	for index in range(1, len(l)):
		mini = min(mini, c[w[l[index] - 1]])
	for index in range(1, len(l)):
		c[w[l[index] - 1]] = mini
f = list(input().split())
res = 0
for i in f:
	res += c[i]
print(res)


(n, k, m) = list(map(int, input().strip().split()))
words = input().strip().split()
cost = list(map(int, input().strip().split()))
cost_dict = dict(zip(words, cost))
for i in range(k):
	l = list(map(int, input().strip().split()))[1:]
	min_cost = cost_dict[words[l[0] - 1]]
	for x in l:
		min_cost = min(cost_dict[words[x - 1]], min_cost)
	for x in l:
		cost_dict[words[x - 1]] = min_cost
message = input().strip().split()
print(sum([cost_dict[x] for x in message]))


(n, k, m) = map(int, input().split())
words = [i for i in input().split()]
cost = [int(i) for i in input().split()]
groups = [[int(i) for i in input().split()] for i in range(k)]
sen = [i for i in input().split()]
groups = [i[1:] for i in groups if i[0] > 1]
dic = dict(zip(list(range(1, n + 1)), cost))
l = [[] for i in range(len(groups))]
for i in range(len(groups)):
	for j in range(len(groups[i])):
		l[i].append(dic.get(groups[i][j]))
for i in range(len(groups)):
	dic.update(dict(zip(groups[i], [min(l[i])] * len(groups[i]))))
dic1 = dict(zip(words, dic.values()))
s = 0
for i in sen:
	s += dic1.get(i)
print(s)


w = list(map(int, input().split(' ')))
d = {}
b = list(map(str, input().split(' ')))
c = 0
s = 0
for i in b:
	c = c + 1
	d[i] = c
a = list(map(int, input().split(' ')))
for i in range(w[1]):
	v = list(map(int, input().split(' ')))
	max = a[v[1] - 1]
	k = v[1] - 1
	for i in range(v[0]):
		if a[v[i + 1] - 1] < max:
			max = a[v[i + 1] - 1]
			k = v[i + 1] - 1
	for i in range(v[0]):
		a[v[i + 1] - 1] = max
o = list(map(str, input().split(' ')))
for i in o:
	s = s + a[d[i] - 1]
print(s)


(n, k, m) = map(int, input().split())
x = list(map(str, input().split()))
a = list(map(int, input().split()))
groups = []
for i in range(k):
	s = set()
	line = list(map(int, input().split()))
	for j in range(line[0]):
		s.add(line[j + 1])
	groups.append(s)
words = list(map(str, input().split()))
for i in range(k):
	min_cost = 1000000000.0 + 1
	for word_index in groups[i]:
		if a[word_index - 1] < min_cost:
			min_cost = a[word_index - 1]
	for word_index in groups[i]:
		a[word_index - 1] = min_cost
d = dict(zip(x, a))
cost = 0
for i in words:
	cost = cost + d[i]
print(cost)


(n, k, m) = map(int, input().split())
s = list(map(str, input().split()))
a = list(map(int, input().split()))
for i in range(k):
	t = list(map(int, input().split()))
	t = t[1:]
	mini = 10000000000000
	for j in range(len(t)):
		mini = min(a[t[j] - 1], mini)
	for j in range(len(t)):
		a[t[j] - 1] = mini
c = {}
for i in range(len(s)):
	c[s[i]] = a[i]
y = list(map(str, input().split()))
l = 0
for i in range(len(y)):
	l += c[y[i]]
print(l)


(n, k, m) = [int(x) for x in input().split()]
s = input().split()
a = [int(x) for x in input().split()]
for i in range(k):
	temp = [int(i) for i in input().split()]
	x = temp[0]
	b = temp[1:]
	minm = 10 ** 9
	for i in range(x):
		if a[b[i] - 1] <= minm:
			minm = a[b[i] - 1]
	for i in range(x):
		a[b[i] - 1] = minm
start = {s[x]: a[x] for x in range(n)}
final = 0
c = input().split()
for i in range(m):
	final += start[c[i]]
print(final)


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
(n, k, w) = mp()
words = inp().split()
wordInds = {}
for (i, word) in enumerate(words):
	wordInds[word] = i
par = l1d(n)
cost = lmp()
for i in range(k):
	tmp = lmp()[1:]
	mnc = inf
	for j in tmp:
		if cost[j - 1] < mnc:
			mnc = cost[j - 1]
			ind = j - 1
	for j in tmp:
		par[j - 1] = ind
message = inp().split()
totCost = 0
for word in message:
	totCost += cost[par[wordInds[word]]]
print(totCost)


def main():
	(n, k, m) = list(map(int, input().strip().split()))
	words = list(input().strip().split())
	cost = list(map(int, input().strip().split()))
	groupcost = []
	d = {}
	for i in range(k):
		tmp = list(map(int, input().strip().split()))
		mini = cost[tmp[1] - 1]
		for j in tmp[1:]:
			mini = min(mini, cost[j - 1])
		groupcost.append(mini)
		for k in tmp[1:]:
			d[words[k - 1]] = i
	count = 0
	temp = list(input().strip().split())
	for i in temp:
		count += groupcost[d[i]]
	print(count)
main()


from functools import reduce
from sys import stdin
from bisect import bisect_left, bisect_right
import heapq

def main():
	(n, k, m) = map(int, stdin.readline().split())
	words = list(stdin.readline().split())
	dic = dict()
	costs = list(map(int, stdin.readline().split()))
	for _ in range(k):
		gp = list(map(int, stdin.readline().split()))[1:]
		min_cst = costs[min(gp, key=lambda i: costs[i - 1]) - 1]
		for i in gp:
			dic[words[i - 1]] = min_cst
	msg = stdin.readline().split()
	cst = 0
	for word in msg:
		cst += dic[word]
	print(cst)
main()


def main():
	(N, K, M) = map(int, input().split())
	W = tuple(input().split())
	A = tuple(map(int, input().split()))
	X = tuple((tuple(map(int, input().split())) for _ in [0] * K))
	Msg = tuple(input().split())
	cost = dict()
	for (i, j) in zip(W, A):
		cost[i] = j
	for x in X:
		if x[0] > 1:
			mina = float('inf')
			for i in x[1:]:
				mina = min(mina, A[i - 1])
			for i in x[1:]:
				cost[W[i - 1]] = mina
	ans = 0
	for m in Msg:
		ans += cost[m]
	print(ans)
main()


(n, k, m) = map(int, input().split())
mp = {}
words = input().split()
costs = [int(i) for i in input().split()]
for i in range(n):
	mp[words[i]] = costs[i]
for i in range(k):
	p = [int(t) for t in input().split()]
	for pp in range(1, p[0] + 1):
		p[pp] -= 1
	arr = []
	mn = 10 ** 9 + 7
	for j in range(1, p[0] + 1):
		d = p[j]
		if costs[d] < mn:
			mn = costs[d]
	for w in range(1, p[0] + 1):
		mp[words[p[w]]] = mn
ans = 0
for i in input().split():
	ans += mp[i]
print(ans)


(n, k, m) = map(int, input().split())
d = {}
s = input().split()
for i in range(n):
	d[s[i]] = i
cost = [0] * n
a = list(map(int, input().split()))
for j in range(n):
	cost[j] = 1 << 30
group = [0] * n
for i in range(k):
	x = list(map(int, input().split()))
	v = 1
	while v < len(x):
		group[x[v] - 1] = i
		cost[i] = min(cost[i], a[x[v] - 1])
		v += 1
ans = 0
y = input().split()
for i in range(m):
	ans += cost[group[d[y[i]]]]
print(ans)


(n, k, m) = map(int, input().split())
w = list(input().split())
lk = {}
for i in range(n):
	lk[w[i]] = i
c = list(map(int, input().split()))
m = [10 ** 19] * k
gr = [0] * n
for i in range(k):
	g = list(map(int, input().split()))
	for j in g[1:]:
		m[i] = min(m[i], c[j - 1])
		gr[j - 1] = i
let = input().split()
res = 0
for word in let:
	res += m[gr[lk[word]]]
print(res)


(n, k, m) = map(int, input().split())
words = input().split()
cost = list(map(int, input().split()))
new_cost = {}
for _k in range(k):
	a = list(map(int, input().split()))[1:]
	c = min((cost[i - 1] for i in a))
	for i in a:
		new_cost[words[i - 1]] = c
text = input().split()
res = 0
for word in text:
	res += new_cost[word]
print(res)


(n, k, m) = map(int, input().split())
words = input().split()
indexes = {}
for i in range(n):
	indexes[words[i]] = i
ind = [-1] * n
num = list(map(int, input().split()))
groups = [[] for i in range(k)]
for i in range(k):
	smth = list(map(int, input().split()))
	for j in range(1, len(smth)):
		a = smth[j] - 1
		groups[i].append(num[a])
		ind[a] = i
for i in range(k):
	groups[i].sort()
new = input().split()
ans = 0
for i in range(len(new)):
	ans += groups[ind[indexes[new[i]]]][0]
print(ans)


s0 = input().split()
n = int(s0[0])
k = int(s0[1])
m = int(s0[2])
s = input().split()
dct = {}
j = 1
for i in s:
	dct[i] = j
	j += 1
ans = 0
s2 = input().split()
cost = {}
for i in range(1, len(s2) + 1):
	cost[i] = int(s2[i - 1])
for i in range(k):
	curs = input().split()
	cursint = []
	for c in curs:
		cursint.append(int(c))
	x = cursint.pop(0)
	min = cost[cursint[0]]
	for j in cursint:
		if cost[j] < min:
			min = cost[j]
	for j in cursint:
		cost[j] = min
s3 = input().split()
for slovo in s3:
	ans += cost[dct[slovo]]
print(ans)


(n, k, m) = map(int, input().split())
a = {}
d = []
for i in input().split():
	a[i] = 0
	d.append(i)
b = list(map(int, input().split()))
j = 0
for i in a:
	a[i] = b[j]
	j += 1
for i in range(k):
	c = list(map(int, input().split()))
	m = 1000000001
	for j in range(c[0]):
		q = a[d[c[j + 1] - 1]]
		if q < m:
			m = q
	for j in range(c[0]):
		a[d[c[j + 1] - 1]] = m
ans = 0
for i in input().split():
	ans += a[i]
print(ans)


(n, k, m) = map(int, input().split(' '))
w2score = {}
id2w = {}
words = input().split(' ')
for (i, w) in enumerate(words):
	w2score[w] = i + 1
	id2w[i + 1] = w
score = list(map(int, input().split(' ')))
for i in range(k):
	group_help = list(map(int, input().split(' ')))
	currentGroup = group_help[1:]
	m = 1000000000
	for j in currentGroup:
		m = min(m, score[j - 1])
	for j in currentGroup:
		w2score[id2w[j]] = m
s = input().split(' ')
out = 0
for x in s:
	out += w2score[x]
print(out)


TN = 1

def solution():
	(n, k, m) = map(int, input().split())
	sl = input().split()
	d_sl = {sl[i]: i + 1 for i in range(n)}
	a = [-1] + list(map(int, input().split()))
	ans = 0
	m_x = max(a)
	min_s = [m_x] * (n + 1)
	for i in range(k):
		s = list(map(int, input().split()))
		x = m_x
		sx = s[1:]
		for j in sx:
			if a[j] < x:
				x = a[j]
		for j in sx:
			min_s[j] = x
	st = input().split()
	for i in st:
		ans += min_s[d_sl[i]]
	print(ans)
while TN != 0:
	solution()
	TN -= 1


import sys
(n, k, m) = list(map(int, input().split()))
words = {}
arr = list(map(str, input().split()))
values = list(map(int, input().split()))
for i in range(n):
	words[arr[i]] = values[i]
(d, f) = ({}, {})
new = {}
for i in range(k):
	piko = list(map(int, input().split()))
	piko = piko[1:]
	d[i] = []
	f[i] = 0
	temp = sys.maxsize
	for j in piko:
		d[i].append(arr[j - 1])
		temp = min(temp, values[j - 1])
		new[arr[j - 1]] = i
	f[i] = temp
l = list(map(str, input().split()))
ans = 0
for i in l:
	ans += f[new[i]]
print(ans)


from fractions import Fraction
from collections import defaultdict
import math
import os
import sys
from io import BytesIO, IOBase
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
(n, k, m) = [int(x) for x in input().split()]
s = list([x for x in input().split()])
a = list([int(x) for x in input().split()])
for i in range(k):
	l = list([int(x) for x in input().split()])
	l.pop(0)
	nl = []
	for j in l:
		nl.append(a[j - 1])
	mi = min(nl)
	for j in l:
		a[j - 1] = mi
s2 = list([x for x in input().split()])
d = {}
for i in range(n):
	d[s[i]] = a[i]
tot = 0
for i in s2:
	tot += d[i]
print(tot)


def find_min_cost(group_words, costs):
	cost_min = int(1000000000000.0)
	for word_indice in group_words:
		cost_min = min(cost_min, costs[word_indice - 1])
	return cost_min
(n, k, m) = list(map(int, input().split()))
words = input().split()
costs = list(map(int, input().split()))
group_words = []
d = {}
for i in range(k):
	tmp = list(map(int, input().split()))
	(x, group) = (tmp[0], tmp[1:])
	cost_min = find_min_cost(group, costs)
	for word_indice in group:
		word = words[word_indice - 1]
		d[word] = cost_min
message = input().split()
total_cost = 0
for word in message:
	total_cost += d[word]
print(total_cost)


class CodeforcesTask959BSolution:

	def __init__(self):
		self.result = ''
		self.n_k_m = []
		self.lang = []
		self.prices = []
		self.groups = []
		self.message = []

	def read_input(self):
		self.n_k_m = [int(x) for x in input().split(' ')]
		self.lang = input().split(' ')
		self.prices = [int(x) for x in input().split(' ')]
		for _ in range(self.n_k_m[1]):
			self.groups.append([int(x) for x in input().split(' ')[1:]])
		self.message = input().split(' ')

	def process_task(self):
		groups = {}
		group_prices = {}
		for g in range(self.n_k_m[1]):
			for word in self.groups[g]:
				groups[self.lang[word - 1]] = g
				if g in group_prices:
					group_prices[g] = min(group_prices[g], self.prices[word - 1])
				else:
					group_prices[g] = self.prices[word - 1]
		price = 0
		for word in self.message:
			price += group_prices[groups[word]]
		self.result = str(price)

	def get_result(self):
		return self.result
Solution = CodeforcesTask959BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


(n, k, m) = input().split()
n = int(n)
k = int(k)
m = int(m)
a = input().split(' ')
b = [int(i) for i in input().split(' ')]
for i in range(k):
	x = [int(j) for j in input().split(' ')]
	mn = b[x[1] - 1]
	for j in range(1, len(x)):
		mn = min(mn, b[x[j] - 1])
	for j in range(1, len(x)):
		b[x[j] - 1] = mn
m = {}
for i in range(n):
	m[a[i]] = b[i]
a = input().split(' ')
ans = 0
for i in a:
	ans += m[i]
print(ans)


(n, k, m) = map(int, input().split())
nw = input().split()
an = list(map(int, input().split()))
group = {}
min_cost = []
te9 = int(10000000000.0)
for i in range(k):
	g = list(map(int, input().split()))
	mc = te9
	for j in g[1:]:
		group[nw[j - 1]] = i
		mc = mc if an[j - 1] > mc else an[j - 1]
	min_cost.append(mc)
mw = input().split()
r = 0
for w in mw:
	r += min_cost[group[w]]
print(r)


(n, k, m) = input().split(' ')
(n, k, m) = [int(n), int(k), int(m)]
words = [x for x in input().split(' ')]
cost = [int(x) for x in input().split(' ')]
grp = {}
mini = [1000000010] * k
for i in range(k):
	g = [int(x) for x in input().split(' ')]
	for j in range(g[0]):
		grp[words[g[j + 1] - 1]] = i
		mini[i] = min(mini[i], cost[g[j + 1] - 1])
res = 0
for s in input().split(' '):
	res += mini[grp[s]]
print(res)


(n, k, m) = [int(i) for i in input().split()]
origwordarr = input().split()
costarr = [int(i) for i in input().split()]
for i in range(k):
	group = [int(i) for i in input().split()]
	if group[0] == 1:
		continue
	minindex = min((i - 1 for i in group[1:]), key=lambda l: costarr[l])
	for j in group[1:]:
		costarr[j - 1] = costarr[minindex]
costdict = dict(zip(origwordarr, costarr))
destwordarr = input().split()
print(sum((costdict[i] for i in destwordarr)))


(n, k, m) = map(int, input().split())
words = input().split()
c = list(map(int, input().split()))
cg = [0] * k
gw = dict()
for i in range(k):
	g = list(map(int, input().split()))[1:]
	mm = 10 ** 9
	for j in g:
		mm = min(mm, c[j - 1])
		gw[words[j - 1]] = i
	cg[i] = mm
ms = input().split()
ans = 0
for i in ms:
	ans += cg[gw[i]]
print(ans)


(n, k, m) = map(int, input().split(' '))
words = list(input().split(' '))
cost = list(map(int, input().split(' ')))
wordGroups = {}

def getVal(x, t):
	x = int(x) - 1
	wordGroups[words[x]] = t
	return cost[x]
costGroups = []
for t in range(k):
	group = sorted([getVal(x, t) for x in input().split(' ')[1:]])
	costGroups.append(group)
message = list(input().split(' '))
totalcost = 0
for word in message:
	totalcost += costGroups[wordGroups[word]][0]
print(totalcost)


from sys import stdin
(words, groups, n) = map(int, stdin.readline().split())
words = stdin.readline().split()
price = dict()
pr = list(map(int, stdin.readline().split()))
for i in range(len(words)):
	price[words[i]] = pr[i]
for i in range(groups):
	group = list(map(int, stdin.readline().split()))[1:]
	minpr = 10 ** 18
	for elem in group:
		minpr = min(minpr, price[words[elem - 1]])
	for elem in group:
		price[words[elem - 1]] = minpr
mess = stdin.readline().split()
res = 0
for elem in mess:
	res += price[elem]
print(res)


getInputList = lambda : list(input().split())
getInputIntList = lambda : list(map(int, input().split()))
(n, k, m) = getInputIntList()
arr = getInputList()
arr_i = getInputIntList()
groups = []
for i in range(k):
	a = getInputIntList()
	if a[0] == 1:
		continue
	else:
		cost = []
		for j in a[1:]:
			cost.append(arr_i[j - 1])
		mi = min(cost)
		for j in a[1:]:
			arr_i[j - 1] = mi
myset = {}
for i in range(n):
	myset[arr[i]] = arr_i[i]
arr_o = getInputList()
ans = 0
for i in arr_o:
	if i in myset:
		ans += myset[i]
print(ans)


s = input().split(' ')
n = int(s[0])
k = int(s[1])
m = int(s[2])
s = input().split(' ')
indexes = range(0, len(s))
costs = [int(i) for i in input().split(' ')]
dic = dict(zip(s, indexes))
min_cost = []
group = {}
for i in range(k):
	s = input().split(' ')
	x = int(s[0])
	cost = 99999999999
	min_cost += [cost]
	for j in range(x):
		word = int(s[j + 1])
		word -= 1
		group[word] = i
		min_cost[i] = min(min_cost[i], costs[word])
message = input().split(' ')
ans = 0
for i in message:
	ans += min_cost[group[dic[i]]]
print(ans)


def solve(words, costs, groups, msg):
	m = len(msg)
	k = len(groups)
	hash = {}
	for i in range(k):
		minVal = 10 ** 9 + 1
		l = len(groups[i])
		for j in range(l):
			if minVal > costs[groups[i][j] - 1]:
				minVal = costs[groups[i][j] - 1]
		for j in range(l):
			hash[words[groups[i][j] - 1]] = minVal
	ret = 0
	for i in range(m):
		ret += hash[msg[i]]
	return ret

def driver():
	inp = [*map(lambda x: int(x), input().split())]
	(n, k, m) = (inp[0], inp[1], inp[2])
	words = input().split()
	costs = [*map(lambda x: int(x), input().split())]
	groups = [[] for _ in range(k)]
	for i in range(k):
		inp = [*map(lambda x: int(x), input().split())]
		for j in range(1, inp[0] + 1):
			groups[i].append(inp[j])
	msg = input().split()
	print(solve(words, costs, groups, msg))
driver()


[n, k, m] = [int(x) for x in input().split(' ')]
words = input().split(' ')
costs = [int(x) for x in input().split(' ')]
groups = []
for _ in range(k):
	groups.append([int(x) for x in input().split(' ')][1:])
message = input().split(' ')
min_word_costs = {}
for group in groups:
	min_cost = min([costs[word_id - 1] for word_id in group])
	for word_id in group:
		if word_id not in min_word_costs or min_cost < min_word_costs[word_id]:
			min_word_costs[word_id] = min_cost
min_word_costs_v2 = {}
for k in min_word_costs:
	min_word_costs_v2[words[k - 1]] = min_word_costs[k]
cost = 0
for msg_word in message:
	cost += min_word_costs_v2[msg_word]
print(cost)


(n, k, m) = map(int, input().split(' '))
lw = list(input().split(' '))
lp = list(map(int, input().split(' ')))
for i in range(k):
	tl = list(map(lambda x: int(x) - 1, input().split(' ')))
	tl.pop(0)
	tpl = list()
	for i in tl:
		tpl.append(lp[i])
	m = min(tpl)
	for i in tl:
		lp[i] = m
el = list(input().split(' '))
d = dict()
for i in range(n):
	d[lw[i]] = lp[i]
s = 0
for i in el:
	s += d[i]
print(s)


inf = 1000 * 1000 * 1000 + 7
(n, k, m) = [int(x) for x in input().split()]
s = input().split()
a = [int(x) for x in input().split()]
mt = dict()
mn = [inf for i in range(k)]
for i in range(k):
	c = [int(x) for x in input().split()]
	for j in range(1, len(c)):
		mt[s[c[j] - 1]] = i
		mn[i] = min(mn[i], a[c[j] - 1])
mes = input().split()
ans = 0
for w in mes:
	ans += mn[mt[w]]
print(ans)


(n, k, m) = map(int, input().split())
l = list(input().split())
a = list(map(int, input().split()))
d = {}
for i in range(k):
	(ac, *c) = map(int, input().split())
	k = 10 ** 9
	for i in c:
		k = min(k, a[i - 1])
	for i in c:
		a[i - 1] = k
for (i, j) in zip(l, a):
	d[i] = j
mes = list(input().split())
sum = 0
for i in mes:
	sum += d[i]
print(sum)


def mp():
	return map(int, input().split())
(n, k, m) = mp()
ph = input().split()
cost = list(mp())
dict = {ph[i]: cost[i] for i in range(n)}
for i in range(k):
	x = list(mp())
	minn = 1000000000
	for j in range(1, len(x)):
		minn = min(cost[x[j] - 1], minn)
	for j in range(1, len(x)):
		dict[ph[x[j] - 1]] = minn
a = input().split()
s = 0
for i in a:
	s += dict[i]
print(s)


(n, k, m) = [int(x) for x in input().split(' ')]
words = input().split(' ')
costs = [int(x) for x in input().split(' ')]
synonims = []
for i in range(k):
	synonims.append([int(x) for x in input().split(' ')][1:])
for group in synonims:
	mins = 1000000000.0
	for el in group:
		mins = min(costs[el - 1], mins)
	for el in group:
		costs[el - 1] = mins
a = dict(zip(words, costs))
message = input().split(' ')
print(sum([a[x] for x in message]))


(n, k, m) = map(int, input().split())
a = list(map(str, input().split()))
b = list(map(int, input().split()))
for i in range(k):
	c = list(map(int, input().split()))
	r = b[c[1] - 1]
	for j in c[1:]:
		r = min(r, b[j - 1])
	for j in c[1:]:
		b[j - 1] = r
c = list(map(str, input().split()))
d = dict(zip(a, b))
su = 0
for i in c:
	su += d[i]
print(su)


(n, k, m) = list(map(int, input().split(' ')))
w = input().split(' ')
ww = {j: i for (i, j) in enumerate(w)}
c = list(map(int, input().split(' ')))
cost = 0
xx = []
for i in range(k):
	x = list(map(int, input().split(' ')))
	min_val = min([c[j - 1] for j in x[1:]])
	for j in x[1:]:
		c[j - 1] = min_val
m = input().split(' ')
mm = [c[ww[i]] for i in m]
print(sum(mm))


(n, k, m) = map(int, input().strip().split())
words = list(input().strip().split())
cost = list(map(int, input().strip().split()))
i = 1
se = {}
minset = {}
while i <= k:
	group = list(map(int, input().strip().split()))
	minv = 10000000000
	for j in range(1, len(group)):
		curword = words[group[j] - 1]
		currcost = cost[group[j] - 1]
		if currcost < minv:
			minv = currcost
		se[curword] = i
	minset[i] = minv
	i += 1
msg = list(input().strip().split())
sum = 0
i = 0
while i < len(msg):
	curgp = se[msg[i]]
	sum += minset[curgp]
	i += 1
print(sum)


arr1 = [int(s) for s in input().split(' ')]
n = arr1[0]
k = arr1[1]
m = arr1[2]
arr2 = [s for s in input().split(' ')]
arr3 = [int(s) for s in input().split(' ')]
dic = {}
for i in range(0, n):
	dic[arr2[i]] = arr3[i]
for i in range(0, k):
	arr4 = [int(s) for s in input().split(' ')]
	minim = dic.get(arr2[arr4[1] - 1])
	for x in range(2, len(arr4)):
		minim = min(minim, dic.get(arr2[arr4[x] - 1]))
	for y in range(1, len(arr4)):
		dic[arr2[arr4[y] - 1]] = minim
arr5 = [s for s in input().split(' ')]
tot = 0
for i in range(0, len(arr5)):
	tot = tot + dic.get(arr5[i])
print(tot)


(n, k, m) = list(map(int, input().strip().split(' ')))
wordlist = list(map(str, input().strip().split(' ')))
wordcost = list(map(int, input().strip().split(' ')))
l = list()
for i in range(k):
	newlist = list(map(int, input().strip().split(' ')))
	l.append(newlist[1:])
message = list(map(str, input().strip().split(' ')))
d = dict()
updcost = list()
for i in range(k):
	mini = float('inf')
	for ele in l[i]:
		mini = min(mini, wordcost[ele - 1])
	for ele in l[i]:
		d[wordlist[ele - 1]] = mini
s = 0
for ele in message:
	s += d[ele]
print(s)


(n, k, m) = [int(i) for i in input().split()]
s = [i for i in input().split()]
cost = [int(i) for i in input().split()]
for i in range(k):
	a = [int(i) for i in input().split()]
	mn = cost[a[1] - 1]
	for j in range(a[0]):
		mn = min(mn, cost[a[1 + j] - 1])
	for j in range(a[0]):
		cost[a[1 + j] - 1] = mn
st = dict()
ct = 0
for i in s:
	st[i] = cost[ct]
	ct += 1
m = input().split()
sum = 0
for i in m:
	sum += st[i]
print(sum)


import sys
input = sys.stdin.readline
mod = 998244353
(n, k, m) = map(int, input().split())
s = [x for x in input().split()]
a = [int(x) for x in input().split()]
f = {}
g = [0 for i in range(n + 100)]
for i in range(n):
	g[i + 1] = a[i]
for i in range(k):
	b = [int(x) for x in input().split()]
	mini = 9899999999994894848
	if len(b) <= 1:
		continue
	b = b[1:]
	for j in range(len(b)):
		if b[j] - 1 >= n:
			continue
		mini = min(mini, a[b[j] - 1])
	for jj in range(len(b)):
		g[b[jj]] = mini
for i in range(len(s)):
	f[s[i]] = i + 1
p = [x for x in input().split()]
c = 0
for i in p:
	c += g[f[i]]
print(c)


(n, k, m) = map(int, input().split())
ids = {}
equiv = {}
last_id = 0
for word in input().split():
	last_id += 1
	ids[word] = last_id
	equiv[last_id] = last_id
costs = list(map(int, ('0 ' + input()).split()))
for i in range(k):
	equiv_list = list(map(int, input().split()))
	for j in range(len(equiv_list) - 1):
		equiv[equiv_list[j + 1]] = equiv_list[1]
		costs[equiv_list[1]] = min(costs[equiv_list[1]], costs[equiv_list[j + 1]])
result = 0
for word in input().split():
	result += costs[equiv[ids[word]]]
print(result)


import sys
import math
(n, k, m) = map(int, input().split())
a = list(input().split())
b = list(map(int, input().split()))
for i in range(k):
	d = list(map(int, input().split()))
	if d[0] == 0:
		continue
	mini = b[d[1] - 1]
	for j in range(2, d[0] + 1):
		mini = min(mini, b[d[j] - 1])
	for q in range(1, d[0] + 1):
		b[d[q] - 1] = mini
c = {}
for l in range(n):
	c[a[l]] = b[l]
e = list(input().split())
o = 0
for p in e:
	o += c[p]
print(o)


def find_min(index, cost):
	mini_cost = cost[index[0] - 1]
	for i in index:
		if cost[i - 1] < mini_cost:
			mini_cost = cost[i - 1]
	return mini_cost

def main():
	(n, k, m) = map(int, input().split())
	s = input().split()
	cost = list(map(int, input().split()))
	group_cost = {}
	group = {}
	for i in range(1, k + 1):
		elem = list(map(int, input().split()))
		index = []
		for j in range(elem[0]):
			index.append(elem[j + 1])
		group_cost[i] = find_min(index, cost)
		for j in index:
			group[s[j - 1]] = i
	total_cost = 0
	message = input().split()
	for j in message:
		total_cost += group_cost[group[j]]
	print(total_cost)
main()


(n, k, m) = map(int, input().split())
words = [x for x in input().split()]
cost = [int(c) for c in input().split()]
groups_dict = {}
minCost = {}
for i in range(k):
	g = [int(x) for x in input().split()[1:]]
	minC = 10 ** 9
	for x in g:
		minC = min(cost[x - 1], minC)
		groups_dict[words[x - 1]] = i
	minCost[i] = minC
ans = 0
for w in input().split():
	ans += minCost[groups_dict[w]]
print(ans)


import math
l1 = list(map(int, input().split()))
n = l1[0]
k = l1[1]
words = list(map(str, input().split()))
cost = list(map(int, input().split()))
dicti = {}
total = 0
for i in range(len(words)):
	word = words[i]
	dicti[word] = i + 1
for i in range(k):
	group = list(map(int, input().split()))
	size = group[0]
	min_cost = math.inf
	if size != 1:
		for j in range(1, len(group)):
			curr_cost = cost[group[j] - 1]
			if curr_cost < min_cost:
				min_cost = curr_cost
		for j in range(1, len(group)):
			cost[group[j] - 1] = min_cost
req = list(map(str, input().split()))
for i in range(0, len(req)):
	curr_word = req[i]
	ind = dicti[curr_word]
	total += cost[ind - 1]
print(total)


def solve(n, k, m, words, costs, similarGroups, message):
	wordCost = {}
	index = 0
	while index < k:
		tar = similarGroups[index]
		innerIndex = 1
		minimum = 10000000001
		while innerIndex <= tar[0]:
			if costs[tar[innerIndex] - 1] < minimum:
				minimum = costs[tar[innerIndex] - 1]
			innerIndex += 1
		innerIndex -= 1
		while innerIndex > 0:
			w = words[tar[innerIndex] - 1]
			wordCost[w] = minimum
			innerIndex -= 1
		index += 1
	ans = 0
	for i in message:
		ans += wordCost[i]
	return ans
(n, k, m) = list(map(int, input().split()))
words = input().split()
costs = list(map(int, input().split()))
sG = []
for x in range(k):
	sG.append(list(map(int, input().split())))
m = input().split()
print(solve(n, k, m, words, costs, sG, m))