#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  map<long long, long long> m1;
  for (long long i = 0; i < (long long)n; i++) {
    long long a, x;
    cin >> a >> x;
    m1[a] = x;
  }
  long long m;
  cin >> m;
  map<long long, long long> m2;
  for (long long i = 0; i < (long long)m; i++) {
    long long b, y;
    cin >> b >> y;
    m2[b] = y;
  }
  long long ans = 0;
  for (auto tmp : m1) {
    long long a = tmp.first;
    long long x = tmp.second;
    if (!m2.count(a))
      ans += x;
    else {
      if (m2[a] < x) ans += x;
    }
  }
  for (auto tmp : m2) {
    long long b = tmp.first;
    long long y = tmp.second;
    if (!m1.count(b))
      ans += y;
    else {
      if (m1[b] <= y) ans += y;
    }
  }
  cout << ans << endl;
}
