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