#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
typedef long long ll;


int main(){

    while(true){
        int n,m;
        cin>>n>>m;
        if(n==0&&m==0)break;
        
        map<string,ll> tosen;
        REP(i,n){
            string s;
            ll cost;
            cin>>s>>cost;
            tosen[s]=cost;
        }
        
        ll ans=0;
        REP(i,m){
            string s;
            cin>>s;
            for(auto itr=tosen.begin();itr!=tosen.end();itr++){
                string tos=itr->first;
                REP(j,tos.size()){
                    if(tos[j]!='*'&&tos[j]!=s[j])goto nextf;
                }
                ans+=itr->second;
            nextf:;
            }
        }
        
        cout<<ans<<endl;
        
    }
    
    return 0;
}