#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a, b;
int main() {
  int i, j, n, m, ind, inc;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &ind, &inc);
    a.push_back(make_pair(ind, inc));
  }
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &ind, &inc);
    b.push_back(make_pair(ind, inc));
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  long long ans = 0;
  i = j = 0;
  while (i < n && j < m) {
    if (a[i].first == b[j].first) {
      ans += max(a[i].second, b[j].second);
      i++;
      j++;
    } else if (a[i].first < b[j].first) {
      ans += a[i].second;
      i++;
    } else {
      ans += b[j].second;
      j++;
    }
  }
  while (i < n) {
    ans += a[i].second;
    i++;
  }
  while (j < m) {
    ans += b[j].second;
    j++;
  }
  printf("%lld\n", ans);
  return 0;
}
