#include <bits/stdc++.h>
using namespace std;
int main() {
  map<int, int> m;
  map<int, int>::iterator it;
  long long i, a, b, s = 0;
  int n;
  cin >> n;
  while (n--) {
    cin >> a >> b;
    m[a] = b;
  }
  int p;
  cin >> p;
  while (p--) {
    cin >> a >> b;
    if (m[a] < b) m[a] = b;
  }
  for (it = m.begin(); it != m.end(); it++) {
    s += it->second;
  }
  cout << s;
  return 0;
}
