#include <bits/stdc++.h>
using namespace std;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
template <typename T>
T pow(T a, T b, long long m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans % m;
}
const long long N = 1e6 + 5;
long long n, m;
long long a[N], b[N], x[N], y[N];
map<long long, long long> ans;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i] >> x[i];
    ans[a[i]] = x[i];
  }
  cin >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> b[i] >> y[i];
    ans[b[i]] = max(ans[b[i]], y[i]);
  }
  long long answer = 0;
  for (auto it : ans) answer += it.second;
  cout << answer;
  return 0;
}
