#include <bits/stdc++.h>
using namespace std;
void ins(long long int a[], long long int n, long long int i = 0) {
  for (long long int i = 0; i < n; i++) a[i] = i;
}
template <typename t>
void ins(t a[], long long int n, t i = 0) {
  for (long long int i = 0; i < n; i++) a[i] = i;
}
void print(long long int a[], long long int n) {
  for (long long int i = 0; i < n; i++) cout << a[i] << " ";
  ;
  cout << endl;
}
template <typename t>
void print(t a[], long long int n) {
  for (long long int i = 0; i < n; i++) cout << a[i] << " ";
  ;
  cout << endl;
}
void po(long long int a[], long long int n) {
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
}
template <typename t>
void po(t a[], long long int n) {
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
}
int main() {
  long long int n;
  cin >> n;
  map<long long int, long long int> ma;
  long long int a, x;
  for (long long int i = 0; i < n; i++) {
    cin >> a >> x;
    ma.insert(pair<long long int, long long int>(a, x));
  }
  map<long long int, long long int> mb;
  long long int b, y;
  long long int m;
  cin >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> b >> y;
    mb.insert(pair<long long int, long long int>(b, y));
  }
  map<long long int, long long int>::iterator ii;
  map<long long int, long long int>::iterator it;
  long long int sum1 = 0;
  for (it = ma.begin(); it != ma.end(); it++) {
    ii = mb.find(it->first);
    if (ii != mb.end()) {
      if (it->second < ii->second) {
        sum1 += it->second;
      } else {
        sum1 += ii->second;
      }
    }
  }
  map<long long int, long long int>::iterator it1;
  map<long long int, long long int>::iterator it2;
  long long int sum = 0;
  for (it1 = ma.begin(); it1 != ma.end(); it1++) {
    sum = sum + (it1->second);
  }
  for (it2 = mb.begin(); it2 != mb.end(); it2++) {
    sum = sum + (it2->second);
  }
  cout << sum - sum1;
  return 0;
}
