#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d", &n);
  vector<int> a(n), x(n);
  long long ans = 0;
  set<pair<int, int> > pa;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a[i], &x[i]);
    pa.insert({a[i], i});
    ans += x[i];
  }
  scanf("%d", &m);
  vector<int> b(m), y(m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &b[i], &y[i]);
    auto it = pa.lower_bound({b[i], -1});
    if (it != pa.end() && it->first == b[i]) {
      int p = it->second;
      ans += max(0, y[i] - x[p]);
    } else
      ans += y[i];
  }
  cout << ans << endl;
}
