#include <bits/stdc++.h>
using namespace std;
int main() {
  long long f, sumf = 0;
  cin >> f;
  vector<pair<long, long> > forces(f);
  for (int i = 0; i < f; i++) {
    cin >> forces[i].first >> forces[i].second;
    sumf += forces[i].second;
  }
  long long t;
  cin >> t;
  forces.resize(f + t);
  for (int i = f; i < t + f; i++) {
    cin >> forces[i].first >> forces[i].second;
    sumf += forces[i].second;
  }
  sort(forces.begin(), forces.end());
  for (int j = 0; j < f + t - 1; j++) {
    if (forces[j].first == forces[j + 1].first) {
      sumf -= min(forces[j].second, forces[j + 1].second);
    }
  }
  cout << sumf;
  return 0;
}
