#include <bits/stdc++.h>
using namespace std;
map<int, int> f;
int main() {
  int n, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (f.find(x) == f.end()) {
      f[x] = y;
    } else {
      f[x] = max(y, f[x]);
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (f.find(x) == f.end()) {
      f[x] = y;
    } else {
      f[x] = max(y, f[x]);
    }
  }
  long long ans = 0;
  for (auto it = f.begin(); it != f.end(); it++) {
    ans += 1LL * it->second;
  }
  cout << ans;
}
