#include <stdio.h>
#include <math.h>

  char nnums[100][9];


int ascnt(int index){
  int i=0;
  //printf("%d:%c\n",index,nnums[index][i]);

  while (nnums[index][i] =='*') {
    i++;
    //printf("%d:%c\n",index,nnums[index][i]);
  }
  return i;
}
int asint(int index){
  int i=0;
  while (nnums[index][i] =='*') {i++;}

  return atoi(&nnums[index][i]);
}

int mpow(int i,int c){
  int j,ret=i;
  for (j=1;j<c;j++) ret *=i;
  return ret;
}

int main(){
  int n,m,i,j,sum,tm;
  sum=0;
  int nmnys[100];
  int ms;
  scanf("%d %d",&n,&m);
  while (!(n==0 && m==0)){
    sum =0;
    for (i=0;i<n;i++)
      scanf("%s %d",nnums[i],&nmnys[i]);

    for (i=0;i<m;i++){
      scanf("%d", &ms);
       for(j=0;j<n;j++){
	//printf("test:%d\n",(ms % (ms/mpow(10,8-ascnt(j))*mpow(10,8-ascnt(j)))));
	 //tm=pow(10,6);
	 tm=(int)pow((double)10,(double)(8-ascnt(j)));
	if (ms % (ms/tm*tm) == asint(j)){
	  sum += nmnys[j];
	  break;
	}
      }
    }
    printf("%d\n",sum);
    scanf("%d %d",&n,&m);
  }
  return 0;
}