#include <iostream>
#include <string>
#include <vector>
#define P pair<string,int>
using namespace std;
bool match(string s, string t)
{
  int i;
  for(i=0;i<8;i++){
    if(t[i] != '*' && t[i] != s[i]) break;
  }
  return i == 8;
}
int main()
{
  int n, m, t;
  string s;
  while(cin >> n >> m, n){
    vector< P > v;
    while(n--){
      cin >> s >> t;
      v.push_back( P(s,t) );
    }
    t = 0;
    while(m--){
      cin >> s;
      vector< P >::iterator it = v.begin();
      while(it != v.end()){
	if(match(s,(*it).first)){
	  t += (*it).second;
	}
	it++;
      }
    }
    cout << t << endl;
  }
  return 0;
}