N,M = map(int,input().split()) d = {} for _ in range(N): s,p = input().split() d[s] = int(p) print(sum(d[input()] for _ in range(M)))