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