#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp1;
int main() {
  int n;
  int m;
  cin >> n;
  long long a[100009];
  long long b[100009];
  long long x[100009];
  long long y[100009];
  long long ans;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> x[i];
    mp1[a[i]] = x[i];
  }
  cin >> m;
  for (int j = 0; j < m; j++) {
    cin >> b[j] >> y[j];
    if (mp1[b[j]] == 0) {
      ans += y[j];
    } else {
      if (mp1[b[j]] * 1ll > y[j] * 1ll) {
        ans += mp1[b[j]] * 1ll;
      } else {
        ans += y[j];
      }
      mp1[b[j]] = 0;
    }
  }
  map<long long, long long>::iterator it = mp1.begin();
  for (it; it != mp1.end(); it++) {
    if (it->second != 0) ans += it->second;
  }
  cout << ans;
  return 0;
}
