#include<iostream>
#include<string>
#include<map>
#include<vector>
#define rep(i,a) for(int i=0;i<a;i++)
using namespace std;
int main(){
	int n,m,sum=0;cin>>n;
	map<string,int>tmp;
	vector<int> ne(n),p,q;
	rep(i,n){
		string s;cin>>s>>ne[i];
		tmp[s]=i;
	}
	cin>>m;
	rep(i,m){
		string a,b;cin>>a>>b;
		p.push_back(tmp[a]);q.push_back(tmp[b]);
	}
	rep(i,m)rep(j,m)ne[p[j]]=ne[q[j]]=min(ne[p[j]],ne[q[j]]);
	rep(i,n)sum+=ne[i];
	cout<<sum<<endl;
}