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