#include <bits/stdc++.h>
using namespace std;
map<int, int> a;
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, v;
    scanf("%d%d", &x, &v);
    a[x] = 0;
    a[x] = max(a[x], v);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int x, v;
    scanf("%d%d", &x, &v);
    if (!a[x])
      a[x] = v;
    else
      a[x] = max(a[x], v);
  }
  long long ans = 0;
  for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) {
    ans += (*it).second;
  }
  printf("%I64d\n", ans);
}
