#include <bits/stdc++.h>
using namespace std;
long long n, m;
pair<long long, long long> a[100100], b[100100];
map<long long, long long> m1, m2;
map<long long, int> m3;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    if (!m1.count(a[i].first))
      m1[a[i].first] = a[i].second;
    else
      m1[a[i].first] = max(m1[a[i].first], a[i].second);
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> b[i].first >> b[i].second;
    if (!m2.count(b[i].first))
      m2[b[i].first] = b[i].second;
    else
      m2[b[i].first] = max(m1[b[i].first], b[i].second);
  }
  long long res = 0;
  for (int i = 1; i <= n; i++) {
    if (!m3.count(a[i].first)) {
      res += max(m1[a[i].first], m2[a[i].first]);
      m3[a[i].first]++;
    }
  }
  for (int i = 1; i <= m; i++) {
    if (!m3.count(b[i].first)) {
      res += max(m1[b[i].first], m2[b[i].first]);
      m3[b[i].first]++;
    }
  }
  cout << res;
  return 0;
}
