#include <iostream>
#include <string>

using namespace std;

struct ticket{
	string str;
	int cost;
	int length;
};

int main()
{
	int n, m;
	ticket tic[101];
	string s;
	
	while(1){
		cin >> n >> m;
		if(n == 0 && m == 0){
			break;
		}
		for(int i = 0; i < n; i++){
			cin >> s >> tic[i].cost;
			for(int j = 0; j < 8; j++){
				if(s[j] != '*'){
					tic[i].str = s.substr(j);
					tic[i].length = j;
					break;
				}
			}
		}
		int c = 0;
		for(int i = 0; i < m; i++){
			cin >> s;
			for(int j = 0; j < n; j++){
				if(s.substr(tic[j].length) == tic[j].str){
					c += tic[j].cost;
				}
			}
		}
		cout << c << endl;
	}
	return 0;
}