#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  map<long long, long long> mp;
  int n, m;
  long long x, y, sum = 0;
  cin >> n;
  for (int i = 0, _b = n - 1; i <= _b; i++) {
    cin >> x >> y;
    sum += y;
    mp[x] = y;
  }
  cin >> m;
  for (int i = 0, _b = m - 1; i <= _b; i++) {
    cin >> x >> y;
    if (!mp[x])
      sum += y;
    else {
      long long tmp = mp[x];
      if (tmp <= y) {
        sum += y - tmp;
      }
    }
  }
  cout << (sum) << endl;
  return 0;
}
