I=input N,M=map(int,I().split()) P={} for i in[0]*N:s,p=I().split();P[s]=int(p) print(sum(P[I()]for j in[0]*M))