#include <bits/stdc++.h>
using namespace std;
map<int, int> ma;
int main(int argc, char *argv[]) {
  long long n, m, x, y, t = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    ma[x] = y;
    t += y;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (ma[x] < y) {
      t -= ma[x];
      t += y;
    }
  }
  cout << t;
  return 0;
}
