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