#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int n;
	cin>>n;
	vector<string>in(n);
	map<string,int>tmp;
	vi ne(n);
	rep(i,n)cin>>in[i]>>ne[i];
	rep(i,n)tmp[in[i]]=i;
	int m;
	cin>>m;
	vi in1,in2;
	rep(i,m){
		string a,b;
		cin>>a>>b;
		int c=tmp[a],d=tmp[b];
		in1.pb(c);
		in2.pb(d);
	}
	rep(i,m){
		rep(j,m){
			if(ne[in1[j]]>ne[in2[j]])ne[in1[j]]=ne[in2[j]];
			else if(ne[in1[j]]<ne[in2[j]])ne[in2[j]]=ne[in1[j]];
		}
	}
	int sum=0;
	rep(i,n)sum+=ne[i];
	cout<<sum<<endl;
}