#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
map<int, int> s;
int n, m;
int main() {
  scanf("%d", &n);
  int p, q;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &p, &q);
    s[p] = q;
  }
  scanf("%d", &m);
  int l, r;
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &l, &r);
    if (s[l] != 0) {
      s[l] = max(s[l], r);
    } else
      s[l] = r;
  }
  map<int, int>::iterator it;
  for (it = s.begin(); it != s.end(); it++) {
    ans += it->second;
  }
  printf("%I64d\n", ans);
  return 0;
}
