#include<bits/stdc++.h>
using namespace std;
using ll = long long;
signed main(){
    int n; cin >> n;
    ll inf = 1000000000000000;
    ll dist[5002],x[5001];
    vector<vector<pair<int,int>>>edge(n+1);
    map<string,int>mp;
    int source = n;
    for(int i = 0; i < 5001;i++){
        dist[i] = inf;
    }
    for(int i = 0; i < n;i++){
        string s;
        cin >> s >> x[i];
        mp[s] = i;
        edge[source].push_back(make_pair(i,x[i]));
    }
    int m; cin >> m;
    for(int i = 0; i < m;i++){
        string _s,_t; cin >> _s >> _t;
        int s = mp[_s],t = mp[_t];
        edge[s].push_back(make_pair(t,0));
        edge[t].push_back(make_pair(s,0));
    }
    priority_queue<vector<ll>,vector<vector<ll>>,greater<vector<ll>>>que;
    dist[source] = 0;
    que.push({dist[source],source});
    while(!que.empty()){
        auto top = que.top();
        que.pop();
        ll tdist = top.front();
        ll now = top.back();
        if(dist[now] > tdist)continue;
        for(int i = 0; i < edge[now].size();i++){
            int to =edge[now][i].first;
            int cost = edge[now][i].second;
            if(dist[to] > tdist  + cost){
                dist[to] = tdist + cost;
                que.push({dist[to],to});
            }
        }
    }
    int ans = 0;
    for(int i = 0; i < n;i++) ans += dist[i];
    cout << ans << endl;
}

