#include <bits/stdc++.h>
using namespace std;
long long n, a, x;
map<long long, long long> m;
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  while (n--) cin >> a >> x, m[a] = x;
  cin >> n;
  while (n--) cin >> a >> x, m[a] = max(m[a], x);
  long long ans = 0;
  for (auto p : m) ans += p.second;
  cout << ans;
}
