#include <bits/stdc++.h>
using namespace std;
int main() {
  map<long long, long long> m1;
  map<long long, long long> m2;
  int n, m;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    m1.insert(make_pair(x, y));
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    long long x, y;
    cin >> x >> y;
    m2.insert(make_pair(x, y));
  }
  map<long long, long long>::iterator it;
  long long sum = 0;
  for (it = m1.begin(); it != m1.end(); ++it) {
    if (it->second >= m2[it->first])
      sum += it->second;
    else
      sum += m2[it->first];
  }
  for (it = m2.begin(); it != m2.end(); ++it) {
    if (m1.find(it->first) == m1.end()) sum += it->second;
  }
  cout << sum << endl;
  return 0;
}
