#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

char target[1000][100];
int money[1000];
char str[100];

int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n) {
  REP(i, n) {
    scanf("%s %d", target[i], &money[i]);
  }
  int sum = 0;
  while (m--) {
    scanf("%s", str);
    REP(i, n) {
      REP(j, 8) {
        if (target[i][j] != '*' && target[i][j] != str[j]) { goto next; }
      }
      sum += money[i];
      break;
next:;
    }
  }
  printf("%d\n", sum);
}
}