#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	int n,m;
	while(cin>>n>>m,n||m){
		if(n==0&&m==0)break;
		vector<pair<string,int > > check;
		string kuji;
		ll sum = 0;
		for(int i =0;i <n;i++){
			string tmp;
			int k;
			cin>>tmp>>k;
			check.push_back(mp(tmp,k));
		}
		/*rep(i, check.size()){
			rep(j, check[i].first.length()){
				cout <<check[i].first[j]<<endl;
			}
		}*/
		for(int i =0;i <m;i++){
			cin>>kuji;
			for(int j =0;j <n;j++){
				bool ok =true;
				for(int k =0;k <8;k++){
					if(check[j].first[k]=='*')continue;
					if(check[j].first[k]!=kuji[k]){
						ok =false;
						break;
					}
				}
				if(ok){
					sum +=check[j].second;
				}
			}
		}
		cout <<sum<<endl;
	}
	return 0;
}