#include <bits/stdc++.h>
using namespace std;
struct aha {
  long long ind, val;
  aha() { ind = val = 0; }
} A[100005], B[100005];
long long C[200005], len = 0;
map<long long, long long> Ans;
int main() {
  long long n, m;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> A[i].ind >> A[i].val;
    C[++len] = A[i].ind;
    Ans[A[i].ind] = A[i].val;
  }
  cin >> m;
  for (long long i = 1; i <= m; i++) {
    cin >> B[i].ind >> B[i].val;
    C[++len] = B[i].ind;
    long long a = Ans[B[i].ind];
    Ans[B[i].ind] = max(B[i].val, a);
  }
  sort(C + 1, C + len + 1);
  long long ans = 0;
  for (long long i = 1; i <= len; i++) {
    if (C[i] == C[i + 1]) continue;
    ans += Ans[C[i]];
  }
  cout << ans;
  return 0;
}
