#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<string, int> SI;

int main()
{
//	cut here before submit 
//	freopen ("testcase.lottey_checker", "r", stdin );
	int n, m;
	while (scanf ("%d %d", &n, &m ) && n && m ){
		vector<SI> lottey (n );
		rep (i, n ){
			cin >> lottey[i].first >> lottey[i].second;
		} // end rep

		vector<string> my (m );
		rep (i, m ){
			cin >> my[i];
		} // end rep
		ll sum = 0LL;
		rep (i, n ){
			string l = lottey[i].first;
			rep (j, m ){
				bool found = true;
				rep (k, 8 ){
					if (l[k] == '*' ) continue;
					else
					if (l[k] != my[j][k] ){
						found = false;
						break;
					} // end if
				} // end rep
				if (found ){
					sum += (ll)lottey[i].second;
				} // end if
			} // end rep
		} // end rep
		cout << sum << endl;	
	} // end while
	
	return 0;
}