#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

struct DATA{
  string s;
  int n;
};

bool Search(const char&);
int Compare(string, string);

int main(){
  int i, j, n, m, x, sum;
  string s;
  string::iterator iter;
  vector<string> Lottery;
  DATA hit;
  vector<DATA> Hit;

  while(1){
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    for(i=0; i<n; ++i){
      cin >> s >> x;
      iter = find_if(s.begin(), s.end(), Search);
      s.erase(s.begin(), iter);
      hit.s = s;
      hit.n = x;
      Hit.push_back(hit);
    }
    for(i=0; i<m; ++i){
      cin >> s;
      Lottery.push_back(s);
    }

    for(i=0, sum=0; i<Lottery.size(); ++i){
      for(j=0; j<Hit.size(); ++j){
	if(Compare(Lottery[i], Hit[j].s) == 1)
	  sum += Hit[j].n;
      }
    }
    cout << sum << endl;
    Lottery.clear();
    Hit.clear();
  }

  return 0;
}

bool Search(const char& c){
  if(c == '*') return false;
  else return true;
}

int Compare(string a, string b){
  int i, j;
  for(i=a.size()-b.size(), j=0; j<b.size(); ++i, ++j){
    if(a[i] != b[j]) return 0;
  }
  return 1;
}