#include <bits/stdc++.h>
using namespace std;
bool check(const string &s) {
  string s1 = s;
  reverse(s1.begin(), s1.end());
  if (s == s1) return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, m, x;
  unordered_map<int, int> m1;
  unordered_map<int, int> m2;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> x;
    m1[a[i]] = x;
  }
  cin >> m;
  vector<int> b(m);
  for (int i = 0; i < m; i++) {
    cin >> b[i] >> x;
    m2[b[i]] = x;
  }
  long long sum = 0ll;
  for (int i = 0; i < n; i++) {
    if (m1[a[i]] >= m2[a[i]]) {
      sum += 1ll * m1[a[i]];
    }
  }
  for (int i = 0; i < m; i++) {
    if (m1[b[i]] < m2[b[i]]) {
      sum += 1ll * m2[b[i]];
    }
  }
  cout << sum;
  return 0;
}
