#include <bits/stdc++.h>
using namespace std;

using P = pair<int, int>;

class union_find {
public:
    union_find(int n, vector<int> const& a)
        : par_(n, -1),
          a_(a)
    {}
    void init(int n) {
        par_.assign(n, -1);
        a_.assign(n, -1);
    }

    int root(int x) {
        return par_[x] < 0 ? x : par_[x] = root(par_[x]);
    }

    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if(x == y) {
            return false;
        } else {
            if(par_[x] < par_[y]) {
                par_[x] += par_[y];
                par_[y] = x;
                a_[x] = min(a_[x], a_[y]);
            } else {
                par_[y] += par_[x];
                par_[x] = y;
                a_[y] = min(a_[y], a_[x]);
            }
            return true;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return -par_[root(x)];
    }

    int get(int x) {
        return a_[root(x)];
    }

private:
    std::vector<int> par_;
    vector<int> a_;
};

using P = pair<int, int>;

int main() {
    int N;
    cin >> N;
    map<string, int> idx;
    vector<int> v(N);
    for(int i=0; i<N; ++i) {
        string a;
        int x;
        cin >> a >> x;
        idx[a] = i;
        v[i] = x;
    }
    union_find uf(N, v);

    int M;
    cin >> M;
    for(int i=0; i<M; ++i) {
        string s, t;
        cin >> s >> t;
        uf.unite(idx[s], idx[t]);
    }

    int res = 0;
    for(int i=0; i<N; ++i) {
        res += uf.get(i);
    }
    cout << res << endl;
}
