#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, n, m, t, sum = 0, ans = 0;
  bool f = 0, flag = 0;
  string s;
  cin >> n;
  long long int a[n + 1], x[n + 1];
  map<long long int, long long int> mm;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> x[i];
    mm[a[i]] = x[i];
  }
  cin >> m;
  long long int b[m + 1], y[m + 1];
  for (i = 0; i < m; i++) {
    cin >> b[i] >> y[i];
    if (mm[b[i]] == 0)
      mm[b[i]] = y[i];
    else
      mm[b[i]] = max(mm[b[i]], y[i]);
  }
  map<long long int, long long int>::iterator it;
  ans = 0;
  for (it = mm.begin(); it != mm.end(); it++) {
    ans += it->second;
  }
  cout << ans << endl;
  return 0;
}
