#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, mn;
  long long a, b, ans = 0;
  cin >> n;
  map<long long, long long> m;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    m[a] = b;
  }
  cin >> mn;
  for (int i = 0; i < mn; ++i) {
    cin >> a >> b;
    if (m.find(a) != m.end())
      m[a] = max(m[a], b);
    else
      m[a] = b;
  }
  map<long long, long long>::iterator it = m.begin();
  while (it != m.end()) {
    ans += it->second;
    it++;
  }
  cout << ans << "\n";
}
