#include <bits/stdc++.h>
using namespace std;
int main() {
  long long m, n;
  map<long long, long long> mpa, mpb;
  vector<int> v;
  cin >> n;
  long long a, x, b, y;
  for (int i = 0; i < n; i++) {
    cin >> a >> x;
    mpa[a] = x;
    v.push_back(a);
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> b >> y;
    mpb[b] = y;
    v.push_back(b);
  }
  long long cnt = 0;
  for (int i = 0; i < v.size(); i++) {
    cnt = cnt + max(mpa[v[i]], mpb[v[i]]);
    mpa[v[i]] = 0;
    mpb[v[i]] = 0;
  }
  cout << cnt << "\n";
  return 0;
}
