#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, i, j, k, a, b, s = 0;
  map<long, long long> p;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    p[a] = b;
    if (b > 0) s += b;
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    if ((b > 0)) {
      if (p.find(a) != p.end()) {
        if (p[a] < b) s += b - p[a];
      } else
        s += b;
    }
  }
  cout << s << "\n";
  return 0;
}
