#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
bool f(string a, string b) {
	for (int i = 0; i < 8; i++)if (a[i] != '*'&&a[i] != b[i])return 0; return 1;
}
string p[123]; int q[123];
signed main() {
	while (1) {
		int n, m, sum = 0;
		cin >> n >> m;
		if (!n)return 0;
		for (int i = 0; i < n; i++)cin >> p[i] >> q[i];
		for (int i = 0; i < m; i++) {
			string k; cin >> k;
			for (int j = 0; j < n; j++)if (f(p[j], k))sum += q[j];
		}
		cout << sum << endl;
	}
}
