#include <bits/stdc++.h>
using namespace std;
int main() {
  map<long long, long long> mp, p;
  int n, m;
  cin >> n;
  long long a[n], x[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> x[i];
    mp[a[i]]++;
  }
  cin >> m;
  long long b[m], y[m];
  for (int i = 0; i < m; i++) {
    cin >> b[i] >> y[i];
    mp[b[i]]++;
    p[b[i]] = i;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (mp[a[i]] == 1) {
      ans += x[i];
    } else {
      ans += max(x[i], y[p[a[i]]]);
      y[p[a[i]]] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    ans += y[i];
  }
  cout << ans;
  return 0;
}
