#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m1, x, y;
  cin >> n;
  set<long long int> s;
  long long int sum1 = 0, sum2 = 0;
  map<long long int, long long int> m;
  for (long long int i = 0; i < n; i++) {
    cin >> x >> y;
    m[x] = y;
    s.insert(x);
    sum1 += y;
  }
  set<long long int>::iterator it;
  cin >> m1;
  for (long long int i = 0; i < m1; i++) {
    cin >> x >> y;
    it = s.find(x);
    if (it == s.end())
      sum2 += y;
    else {
      if (m[x] < y) {
        sum1 -= m[x];
        sum2 += y;
      }
    }
  }
  cout << sum1 + sum2 << endl;
}
