#include<iostream>
#include<sstream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>

#define rep(i,n) for(int i=0;i<n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define rp(i,c) rep(i,(c).size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf=1<<28;
const double INF=1e12,EPS=1e-9;

int n,m;
int money[100];
char ptn[100][10],in[10];

int main()
{
	while(scanf("%d%d",&n,&m),n)
	{
		rep(i,n)scanf("%s%d",ptn[i],money+i);
		
		int ans=0;
		rep(i,m)
		{
			scanf("%s",in);
			rep(j,n)
			{
				int k=7; for(;k>=0;k--)
				{
					if(ptn[j][k]=='*')
					{
						k=-1; break;
					}
					if(ptn[j][k]!=in[k])break;
				}
				if(k<0)
				{
					ans+=money[j]; break;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}