#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> P;
typedef std::pair<int,P> State;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

int N, M;
std::string A[100];
int B[100];

bool match(const std::string& a, const std::string& s){
    REP(i, 8){
        if(a[i] == '*' || a[i] == s[i]);
        else{return false;}
    }
    return true;
}

int main(){
    while(std::cin >> N >> M, N){
        REP(i, N){
            std::cin >> A[i] >> B[i];
        }
    
        int res = 0;
        REP(i, M){
            std::string s;
            std::cin >> s;

            REP(j, N){
                if(match(A[j], s)){res += B[j];}
            }
        }

        std::cout << res << std::endl;
    }
}