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: interview

Starter code:

Solutions:
n, k, m = list(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)

import atexit
import io
import sys

# Buffering IO
_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 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)

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)


n,k,m=list(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)



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 = 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 = 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 = 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([wtoi[w] for w in input().split()])

cost = sum([costs[i - 1] for i in message])
print(cost)


def ii():
    return int(input())
def mi():
    return list(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)


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)


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


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()))


n,k,m=list(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
    #print(ind)
    for i in range(1,inp[0]+1):
        A[inp[i]-1]=A[inp[ind]-1]
    #print(A)

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)
    


(n, k, m) = list(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)


n, k, m = list(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)


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)

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)

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())
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)

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 = [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)

n, k, m = list(map(int, input().split()))
lang = input().split()
cost = list(map(int, input().split()))

groups = {}
mincost = {}

for g in range(k):
    x, *ind = list(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)