#include <iostream>
#include <string>
using namespace std;

int main(void)
{
	int n, m;
	string kuji[1024], atari[128];
	int atarinum[128], money[128];
	
	while (cin >> n >> m, n){
		for (int i = 0; i < n; i++) cin >> atari[i] >> money[i];
		
		for (int i = 0; i < n; i++){
			int cnt = 0;
			for (int j = 7; 0 <= j; j--){
				if (atari[i][j] != '*') cnt++;
			}
			atarinum[i] = cnt;
		}
		
		int res = 0;
		for (int i = 0; i < m; i++) {
			cin >> kuji[i];
			for (int j = 0; j < n; j++){
				if (atari[j].substr(8-atarinum[j],atarinum[j]) == kuji[i].substr(8-atarinum[j],atarinum[j])) res += money[j];
			}
		}
		cout << res << endl;
		
	}
	
	return 0;
}