#include <bits/stdc++.h>
struct P {
  int x, y;
} p[200005];
bool cmp(P a, P b) { return a.x != b.x ? a.x < b.x : a.y > b.y; }
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y);
  scanf("%d", &m);
  m += n;
  for (int i = n + 1; i <= m; i++) scanf("%d%d", &p[i].x, &p[i].y);
  std::sort(p + 1, p + m + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= m; i++)
    if (p[i].x != p[i - 1].x) ans += p[i].y;
  printf("%I64d\n", ans);
}
