#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  unordered_map<long long int, long long int> a, b;
  cin >> n;
  long long int ai, xi;
  for (int i = 0; i < n; ++i) {
    cin >> ai >> xi;
    a[ai] = xi;
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> ai >> xi;
    b[ai] = xi;
  }
  long long int outsum = 0;
  for (auto v : a) {
    if (b.find(v.first) != b.end()) {
      outsum += b[v.first] > v.second ? b[v.first] : v.second;
      b[v.first] = 0;
    } else {
      outsum += v.second;
    }
  }
  for (auto v : b) {
    outsum += v.second;
  }
  cout << outsum << endl;
  return 0;
}
