// Template {{{
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;

template<typename T1, typename T2>
ostream& operator << (ostream& os, const pair<T1, T2>& p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template<typename T>
ostream& operator << (ostream& os, const vector<T>& v) {
    os << "{";
    for(int i = 0; i < v.size(); i++)
        os << v[i] << (i+1<v.size()?", ":"");
    os << "}";
    return os;
}

class range {
  struct Iterator {
    int val, inc;
    int operator*() {return val;}
    bool operator!=(Iterator& rhs) {return val < rhs.val;}
    void operator++() {val += inc;}
  };
  Iterator i, n;
public:
  range(int e) : i({0, 1}), n({e, 1}) {}
  range(int b, int e) : i({b, 1}), n({e, 1}) {}
  range(int b, int e, int inc) : i({b, inc}), n({e, inc}) {}
  Iterator& begin() {return i;}
  Iterator& end() {return n;}
};

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
inline bool valid(int x, int w) { return 0 <= x && x < w; }

void iostream_init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.setf(ios::fixed);
    cout.precision(12);
}
//}}}

struct UnionFind {
    vector<int> data;
    UnionFind(int N) : data(N, -1) { }
    // x??¨y???????????????
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    // x??¨y???????????????????????????????????????
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    // x??????????????????????´???°????±???????
    int size(int x) {
        return -data[root(x)];
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
};
int main(){
    iostream_init();
    int N;
    cin >> N;
    UnionFind uf(N);
    map<string,int> index;
    vector<int> val;
    REP(i, N) {
        string s;
        int x;
        cin >> s >> x;
        val.push_back(x);
        index[s] = i;
    }

    int M;
    cin >> M;
    REP(i, M) {
        string a, b;
        cin >> a >> b;
        int x = index[a];
        int y = index[b];
        uf.unite(x, y);
    }

    vector<int> min_val(N, INT_MAX);
    REP(i, N) {
        int r = uf.root(i);
        min_val[r] = min(min_val[r], val[i]);
    }
    int ans = 0;
    REP(i, N) {
        ans += min_val[ uf.root(i) ];
    }
    cout << ans << endl;
    return 0;
}

/* vim:set foldmethod=marker: */