M[100],i,j,h,s;char N[100][9],B[9];main(n,m){for(;scanf("%d%d",&n,&m)*n;printf("%d\n",s)){for(i=n;i--;)scanf("%s%d",N[i],M+i);for(s=0;m--;)for(scanf("%s",B),i=n;i--;s+=M[i]*h)for(h=1,j=8;j--;)if(N[i][j]-42&&N[i][j]-B[j])h=0;}}