#include <bits/stdc++.h>
using namespace std;
long long a[1000005], x[100005], b[100005], y[100005];
int n, m;
map<long long, long long> mp;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  mp.clear();
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> x[i];
    mp[a[i]] = x[i];
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> b[i] >> y[i];
    mp[b[i]] = max(mp[b[i]], y[i]);
  }
  for (int i = 1; i <= n; i++) {
    sum += mp[a[i]];
    mp[a[i]] = 0;
  }
  for (int i = 1; i <= m; i++) {
    sum += mp[b[i]];
  }
  cout << sum << endl;
  return 0;
}
