#include <bits/stdc++.h>
using namespace std;
void inline accelerate_io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
map<long long, long long> ma;
int main() {
  accelerate_io();
  int n;
  long long a, x;
  cin >> n;
  while (n--) {
    cin >> a >> x;
    if (ma.find(a) == ma.end())
      ma[a] = x;
    else
      ma[a] = max(ma[a], x);
  }
  cin >> n;
  while (n--) {
    cin >> a >> x;
    if (ma.find(a) == ma.end())
      ma[a] = x;
    else
      ma[a] = max(ma[a], x);
  }
  long long ans = 0;
  for (auto it = ma.begin(); it != ma.end(); ++it) ans += it->second;
  cout << ans << endl;
}
