import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.util.TreeMap;
class Main {
//UVA 10295
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String m[] = br.readLine().split(" ");
int words = Integer.parseInt(m[0]);
int tc = Integer.parseInt(m[1]);
TreeMap <String, Integer> dic = new TreeMap<String, Integer>();
StringBuffer sb = new StringBuffer("");
for (int i=0 ; i< words; i++){
m = br.readLine().split(" ");
dic.put(m[0], Integer.parseInt(m[1]));
}
String str = br.readLine();
int ctr=0;
while(true){
if(str == null) {
System.out.print(sb);
System.exit(0);
}
if(str.equalsIgnoreCase(".")){
sb.append(ctr).append("\n");
ctr =0;
}
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens()) {
String check = st.nextToken();
//System.out.println("what am i checing" + check);
if(dic.containsKey(check)) {
//System.out.println(ctr);
ctr = ctr + dic.get(check);
}
}
//System.out.println(ctr);
str=br.readLine();
}
}
}
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
import java.util.TreeMap;
class Main {
//UVA 10295
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String m[] = br.readLine().split(" ");
int words = Integer.parseInt(m[0]);
int tc = Integer.parseInt(m[1]);
TreeMap <String, Integer> dic = new TreeMap<String, Integer>();
StringBuffer sb = new StringBuffer("");
for (int i=0 ; i< words; i++){
m = br.readLine().split(" ");
dic.put(m[0], Integer.parseInt(m[1]));
}
String str = br.readLine();
int ctr=0;
while(true){
if(str == null) {
System.out.print(sb);
System.exit(0);
}
if(str.equalsIgnoreCase(".")){
sb.append(ctr).append("\n");
ctr =0;
}
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens()) {
String check = st.nextToken();
//System.out.println("what am i checing" + check);
if(dic.containsKey(check)) {
//System.out.println(ctr);
ctr = ctr + dic.get(check);
}
}
//System.out.println(ctr);
str=br.readLine();
}
}
}