#include <bits/stdc++.h>
#define repi(i,x,n) for(int i = x; i < n; i++)
#define rep(i,n) repi(i,0,n)
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = 1e9 + 7;
map<string, int> mp;
map<string, vector<string>> G;
map<string, int> used;
int dfs(string s, int &sz)
{
    int res = mp[s];
    sz++;
    for(int u = 0; u < (int)G[s].size(); u++)
    {
        if(used[G[s][u]]) continue;
        used[G[s][u]] = 1;
        res = min(res, dfs(G[s][u], sz));
    }
    return res;
}
signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<string> s;
    for(int i = 0; i < n; i++)
    {
        string a; cin >> a;
        s.push_back(a);
        int x; cin >> x;
        mp[a] = x;
    }
    int m; cin >> m;
    for(int i = 0; i < m; i++)
    {
        string s, t; cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    int ans = 0;
    for(int i = 0; i < n; i++)
    {
        if(used[s[i]]) continue;
        used[s[i]] = 1;
        int sz = 0;
        ans += dfs(s[i], sz) * sz;
    }
    cout << ans << endl;
    return 0;
}

