static int N, M;
static queue<pair<string, int>> q;
static list<int> l;
static list<string> v;
static queue<list<string>::iterator> qq;
static int check(string &s1)
{
int same = 0;
list<string>::iterator s, e;
list<int>::iterator ls, le;
for (int pos = 0; s1[pos] != 0; pos++)
{
if (s1[pos] != '*')
l.push_back(pos);
}
e = v.end(); le = l.end();
for (s = v.begin(); s != e; s++)
{
for (ls = l.begin(); ls != le; ls++)
{
if (s1[*ls] != s->at(*ls))
break;
}
if (ls == le)
{
same += 1;
qq.push(s);
}
}
while (!qq.empty())
{
v.erase(qq.front());
qq.pop();
}
l.clear();
return same;
}
static int solution(void)
{
int prize = 0, tmp;
char buf[16];
scanf("%d %d\n", &N, &M);
for (int i = 0; i < N; i++)
{
scanf("%s %d\n", buf, &tmp);
q.push(pair<string, int>(buf, tmp));
}
for (int i = 0; i < M; i++)
{
scanf("%s\n", buf);
v.push_back(buf);
}
while (!q.empty())
{
int cnt = check(q.front().first);
prize += q.front().second * cnt;
q.pop();
}
v.clear();
return prize;
}