#include <bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;
typedef pair<int,pair<int,int>> PP;
typedef long long ll;

const double EPS = 1e-9;
const int INF = 1e9;
const int MOD = 1e9+7;

int dy[] = {0,1,0,-1};
int dx[] = {1,0,-1,0};

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main(void) {
	int N;
	cin >> N;
	map<string,int> id;
	vector<int> x(N);
	for(int i=0;i<N;i++){
		string a;
		cin >> a >> x[i];
		id[a] = i;
	}
	UnionFind uf(N+1);

	int M;
	cin >> M;
	
	for(int i=0;i<M;i++){
		string s,t;
		cin >> s >> t;
		uf.unionSet(id[s],id[t]);
	}

	vector<int> cost(N);
	for(int i=0;i<N;i++)cost[i] = x[i];
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(i == j)continue;
			if(uf.findSet(i,j)){
				cost[i] = min(cost[i],x[j]);
			}
		}
	}
	ll ans = 0;
	for(int i=0;i<N;i++){
		ans += cost[i];
	}
	cout << ans << endl;

	return 0;
}