//include
//------------------------------------------
#include <bits/stdc++.h>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

//container util
//------------------------------------------
#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define iter(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)

//repetition
//------------------------------------------
#define loop(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  loop(i,0,n)

//constant
//--------------------------------------------
const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

// output vector
// ------------------------------------------
namespace std {
    template<typename T> ostream & operator<<(ostream& os, vector<T> const& v){
        for(auto it=v.begin(); it!=v.end(); ++it){
            os << *it << (it+1==v.end() ? "" : ",");
        }
        return os;
    }
}

//clear memory
#define clr(a) memset((a), 0 ,sizeof(a))

bool pairCompare(const std::pair<string, int>& firstElem, const std::pair<string, int>& secondElem) {
    return firstElem.second > secondElem.second;
}

int main(){
    int n, m;
    while(cin >> n >> m, n+m){
        vector<pair<string, int> > t;
        vs h;
        rep(i, n){
            string p; int q;
            cin >> p >> q;
            t.pb(mp(p, q));
        }
        rep(i, m){
            string p;
            cin >> p;
            h.pb(p);
        }
        sort(all(t), pairCompare);

        int ans = 0;
        rep(i, m){
            rep(j, n){
                int star = 0;
                rep(k, t[j].first.size()){
                    if(t[j].first.substr(k, 1) == "*") star++;
                    else break;
                }
                string s = h[i];
                if(s.substr(star) == t[j].first.substr(star)){
                    ans += t[j].second;
                    break;
                }
            }
        }
        cout << ans << endl;
    }
}