#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, k;
  long long int arr[100002], arr2[100002], k1, k2, sum = 0;
  map<int, long long int> m1, m2;
  set<long long int> s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i] >> k1;
    m1[arr[i]] = k1;
  }
  cin >> m;
  for (int j = 0; j < m; j++) {
    cin >> arr2[j] >> k2;
    m2[arr2[j]] = k2;
    s.insert(arr2[j]);
  }
  for (int i = 0; i < n; i++) {
    if (s.find(arr[i]) != s.end()) {
      if (m1[arr[i]] >= m2[arr[i]]) {
        sum = sum + m1[arr[i]];
        m2[arr[i]] = 0;
      } else {
        sum = sum + m2[arr[i]];
        m2[arr[i]] = 0;
      }
    } else {
      sum = sum + m1[arr[i]];
    }
  }
  for (int i = 0; i < m; i++) {
    sum = sum + m2[arr2[i]];
  }
  cout << sum << endl;
  return 0;
}
