#include <bits/stdc++.h>
using namespace std;
signed main() {
  vector<pair<long long, long long> > e;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long p, q;
    cin >> p >> q;
    e.push_back(make_pair(p, q));
  }
  long long m;
  cin >> m;
  for (long long i = 0; i < m; i++) {
    long long p, q;
    cin >> p >> q;
    e.push_back(make_pair(p, q));
  }
  sort(e.begin(), e.end());
  long long tot = 0;
  long long best = e[0].second;
  for (long long i = 1; i < e.size(); i++) {
    if (e[i].first != e[i - 1].first) {
      tot += best;
      best = e[i].second;
    } else {
      best = max(best, e[i].second);
    }
  }
  cout << tot + best;
  return 0;
}
