#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <bitset>
#define MAX 10000
using namespace std;
typedef long long int lli;
int main(int argc, char *argv[]){
int m, n;
string word;
int value;
map<string, int> dictionary;
map<string, int>::iterator it;
cin >> m >> n;
for (int i=0; i<m; ++i){
cin >> word >> value;
dictionary[word]=value;
}
string s;
while (n--){
int salary = 0;
while (cin >> s && s!="."){
it = dictionary.find(s);
if (it!=dictionary.end())
salary += it->second;
}
cout << salary << endl;
}
return 0;
}