#include <bits/stdc++.h>
using namespace std;
int main() {
  unordered_map<int, int> mp;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, x;
    cin >> a >> x;
    mp.insert({a, x});
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int b, y;
    cin >> b >> y;
    if (mp.find(b) != mp.end()) {
      auto itr = mp.find(b);
      if (itr->second < y) {
        itr->second = y;
      }
    } else {
      mp.insert({b, y});
    }
  }
  long long int ans = 0;
  for (auto x : mp) {
    ans += x.second;
  }
  cout << ans << endl;
}
