#include <bits/stdc++.h>
using namespace std;
int main() {
  map<int, int> mp;
  map<int, int>::iterator it;
  int n, m, x, y, z;
  cin >> n;
  while (n--) {
    cin >> x >> y;
    z = mp[x];
    y = max(y, z);
    mp[x] = y;
  }
  cin >> m;
  while (m--) {
    cin >> x >> y;
    z = mp[x];
    y = max(y, z);
    mp[x] = y;
  }
  long long int p = 0;
  for (it = mp.begin(); it != mp.end(); it++) {
    p += it->second;
  }
  cout << p << endl;
  return 0;
}
