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

class UnionFind {
	vector<int> data, mini;
	int root(int a) {
		return data[a] < 0 ? a : data[a] = root(data[a]);
	}
public:
	UnionFind(const vector<int>& mini_) : data(mini_.size(), -1), mini(mini_) {}
	bool Find(int a, int b) {
		return root(a) == root(b);
	}
	void Union(int a, int b) {
		int ra = root(a), rb = root(b);
		if (ra == rb) return;
		if (data[ra] < data[rb]) {
			data[ra] += data[rb];
			data[rb] = a;
			mini[ra] = min(mini[ra], mini[rb]);
		}
		else {
			data[rb] += data[ra];
			data[ra] = b;
			mini[rb] = min(mini[ra], mini[rb]);
		}
	}
	int Mini(int a) {
		return mini[root(a)];
	}
};

int main()
{
	int N, M, res = 0;
	string s, t;
	cin >> N;
	vector<string> a(N);
	vector<int> x(N);
	for (int i = 0; i < N; i++) {
		cin >> a[i] >> x[i];
	}
	UnionFind uf(x);
	cin >> M;
	for (int i = 0, is, it; i < M; i++) {
		cin >> s >> t;
		for (int j = 0; j < N; j++) {
			if (a[j] == s)
				is = j;
			else if (a[j] == t)
				it = j;
		}
		uf.Union(is, it);
	}
	for (int i = 0; i < N; i++) {
		res += uf.Mini(i);
	}
	cout << res << endl;
	return 0;
}