#include <bits/stdc++.h>
using namespace std;
struct mang {
  int cs, gt;
  bool operator<(const mang& x) const { return cs < x.cs; }
};
int n, m;
mang A[1000000], B[1000000], C[1000000];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &A[i].cs, &A[i].gt);
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d%d", &B[i].cs, &B[i].gt);
  for (int i = 1; i <= n; i++) C[i] = A[i];
  for (int i = 1; i <= m; i++) C[n + i] = B[i];
  sort(C + 1, C + n + m + 1);
  long long kq = 0;
  for (int i = 1; i <= n + m - 1; i++)
    if (C[i].cs == C[i + 1].cs) {
      C[i].gt = max(C[i].gt, C[i + 1].gt);
      C[i + 1].gt = 0;
    }
  for (int i = 1; i <= n + m; i++) kq = kq + (long long)C[i].gt;
  cout << kq;
}
