#include <bits/stdc++.h>
using namespace std;
struct comp {
  bool operator()(const pair<int, int> &a, const pair<int, int> &b) {
    return (a.first < b.first) ||
           ((a.first == b.first) && (a.second > b.second));
  }
} cmp;
int main() {
  int n, m, a, x, b, y, last = -1;
  long long int sum = 0;
  vector<pair<int, int> > v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> x;
    v.push_back({a, x});
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b >> y;
    v.push_back({b, y});
  }
  sort(v.begin(), v.end(), cmp);
  for (int i = 0; i < v.size(); i++) {
    if (last != v[i].first) {
      sum += v[i].second;
      last = v[i].first;
    }
  }
  cout << sum << endl;
  return 0;
}
