#include<iostream>
#include<iomanip>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

vector<bool> d;
vector<vector<ll> > r;

int dfs(int roc) {
	if(d[roc]) return 0;
	int res = 1;
	d[roc] = true;
	REP(i, r[roc].size()) {
		if(!d[r[roc][i]]) {
			res += dfs(r[roc][i]);
		}
	}
	return res;
}

int main(void) {
		ll n, m;
		cin>>n;
		vector<pair<ll, string> > c(n);
		REP(i, n) {
			string gomi;
			ll hoge;
			cin>>gomi>>hoge;
			c[i] = {hoge, gomi};
		}
		sort(ALL(c));
		map<string, ll> s;
		REP(i, n) {
			s[c[i].second] = i;
		}
		d.resize(n);
		REP(i, n) d[i] = false;
		cin>>m;
		r.resize(n);
		REP(i, m) {
			string foo1, foo2;
			cin>>foo1>>foo2;
			r[s[foo1]].pb(s[foo2]);
			r[s[foo2]].pb(s[foo1]);
		}
		ll ans = 0;
		REP(i, n) {
			if(!d[i]) {
			int buf = dfs(i);
			ans += buf * c[i].first;
			}
		}
		cout<<ans<<endl;
	
}