#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long n, m, a, b;
  map<long long, long long> mp;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a >> b;
    mp[a] = b;
  }
  cin >> m;
  for (long long i = 0; i < m; i++) {
    cin >> a >> b;
    if (mp.count(a) != 0)
      mp[a] = max(mp[a], b);
    else
      mp[a] = b;
  }
  long long ans = 0;
  for (auto i : mp) {
    ans += i.second;
  }
  cout << ans;
  return 0;
}
