#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
void prc();
int main() {
  prc();
  prc();
  long long sum = 0;
  for (auto z : mp) sum += z.second;
  printf("%lld\n", sum);
}
void prc() {
  int n;
  scanf("%d", &n);
  int a, b;
  for (long long i = 1; i <= n; i++) {
    scanf("%d%d", &a, &b);
    if (mp.count(a) == 0)
      mp[a] = b;
    else if (mp[a] < b)
      mp[a] = b;
  }
}
