#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;

string a[5010];
int b[5010];
int x[5010];
int n, m;
int color[5010];
int minVal[5010];
int id[5010];
vector<vector<int> > g;

int main(){
    while(cin >> n){
        g.resize(n);
        vector<string> ss(n);
        for(int i = 0; i < n; i++){
            color[i] = -1, g[i].clear(), minVal[i] = 1e9;
        }
        for(int i = 0 ; i < n; i++){
            cin >> a[i] >> x[i];
            ss[i] = a[i];
        }
        sort(ss.begin(), ss.end());
        for(int i = 0; i < n; i++){
            int id = lower_bound(ss.begin(), ss.end(), a[i]) - ss.begin();
            b[id] = x[i];
        }
        cin >> m;
        for(int i = 0; i < m; i++){
            string p,q;
            cin >> p >> q;
            int pi = lower_bound(ss.begin(), ss.end(), p) - ss.begin();
            int qi = lower_bound(ss.begin(), ss.end(), q) - ss.begin();
            g[pi].push_back(qi);
            g[qi].push_back(pi);
        }
        int col = 0;
        for(int i = 0; i < n; i++){
            if(color[i] != -1) continue;
            vector<int> S;
            S.push_back(i);
            int m = 1e9;
            while(S.size()){
                int v = S.back(); S.pop_back();
                color[v] = col;
                m = min(m, b[v]);
                for(int j = 0; j < (int)g[v].size(); j++){
                    int d = g[v][j];
                    if(color[d] == -1) S.push_back(d);
                }
            }
            minVal[col] = m;
            col++;
        }
        int ans = 0;
        for(int i = 0; i < n; i++){
            int id = lower_bound(ss.begin(), ss.end(), a[i]) - ss.begin();
            ans += minVal[color[id]];
        }
        cout << ans << endl;
    }
}