#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

struct Data {
  int ID,weight;
  bool operator < ( const Data &data ) const { return weight < data.weight; }
};

const int MAX_V = 5100;
int V,E;
vector<int> G[MAX_V];
bool used[MAX_V];
vector<Data> vec;

void compute(){
  sort(vec.begin(),vec.end());
  int mini = 0;
  rep(i,V) if( !used[vec[i].ID] ) {
    used[vec[i].ID] = true;
    mini += vec[i].weight;
    deque<int> deq;
    deq.push_back(vec[i].ID);
    while( !deq.empty() ){
      int cur = deq.front(); deq.pop_front();
      rep(j,(int)G[cur].size()){
	int next = G[cur][j];
	if( !used[next] ) {
	  used[next] = true;
	  deq.push_back(next);
	  mini += vec[i].weight;
	}
      }
    }
  }
  cout << mini << endl;
}

int main(){
  map<string,int> mp;
  cin >> V;
  rep(i,V) {
    string s;
    int c;
    cin >> s >> c;
    mp[s] = i;
    vec.push_back((Data){i,c});
  }
  cin >> E;
  rep(i,E){
    string s,t;
    cin >> s >> t;
    int a = mp[s], b = mp[t];
    G[a].push_back(b);
    G[b].push_back(a);
  }
  compute();
  return 0;
}