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