#include using namespace std; using ll = long long; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector> a(n); for(auto &&[s, v] : a) cin >> s >> v; sort(a.begin(), a.end()); string t; ll ans = 0; while(m--){ cin >> t; int p = lower_bound(a.begin(), a.end(), make_pair(t, -1)) - a.begin(); ans += a[p].second; } cout << ans << '\n'; }