#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int N, Q, x[5007], group[5007]; string s[5007], t1, t2; vector<int> g[5007];
int key(string str) {
	return find(s, s + N, str) - s;
}
int main() {
	cin >> N;
	for (int i = 0; i < N; i++) cin >> s[i] >> x[i], g[i] = { i }, group[i] = i;
	cin >> Q;
	for (int i = 0; i < Q; i++) {
		cin >> t1 >> t2;
		int r1 = group[key(t1)], r2 = group[key(t2)];
		if (r1 == r2) continue;
		for (auto &e : g[r2]) {
			group[e] = r1;
			g[r1].push_back(e);
		}
		g[r2].clear();
	}
	int ret = 0;
	for (int i = 0; i < N; i++) {
		if (g[i].empty()) continue;
		int res = 999999999;
		for (auto &e : g[i]) res = min(res, x[e]);
		ret += res * g[i].size();
	}
	cout << ret << endl;
	return 0;
}