#include <bits/stdc++.h>
using namespace std;
long long m, n, x, y, z, p, q, r, ans;
map<long long, long long> cnt;
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    scanf("%lld %lld", &x, &y);
    cnt[x] = y;
  }
  cin >> m;
  for (long long i = 1; i <= m; i++) {
    scanf("%lld %lld", &x, &y);
    cnt[x] = max(cnt[x], y);
  }
  for (auto xx : cnt) {
    ans += xx.second;
  }
  cout << ans << endl;
  return 0;
}
