#include <bits/stdc++.h>
using namespace std;
int main() {
  map<int, int> mp;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a, x;
    scanf("%d %d", &a, &x);
    mp[a] = max(mp[a], x);
  }
  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int a, x;
    scanf("%d %d", &a, &x);
    mp[a] = max(mp[a], x);
  }
  long long sum = 0;
  for (auto it : mp) sum += it.second;
  printf("%lld\n", sum);
  return 0;
}
