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