#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, i, j, m, a, b;
  cin >> n;
  long long total = 0;
  map<int, int> x;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    x[a] = b;
  }
  cin >> m;
  map<int, int> y;
  for (i = 0; i < m; i++) {
    cin >> a >> b;
    y[a] = b;
  }
  for (auto const& it : x) {
    if (y[it.first]) {
      total += (long long)max(it.second, y[it.first]);
    } else {
      total += (long long)it.second;
    }
  }
  for (auto const& it : y) {
    if (x[it.first]) {
    } else {
      total += (long long)it.second;
    }
  }
  cout << total << endl;
  return 0;
}
