#include <bits/stdc++.h>
using namespace std;
int main() {
  unordered_map<int, int> cnt;
  int n, a, x;
  cin >> n;
  for (int i = 0; i < n; i += 1) {
    cin >> a >> x;
    cnt[a] = max(cnt[a], x);
  }
  int m, b, y;
  cin >> m;
  for (int i = 0; i < m; i += 1) {
    cin >> b >> y;
    cnt[b] = max(cnt[b], y);
  }
  long long sum = 0;
  for (auto itr : cnt) sum += itr.second;
  cout << sum << endl;
}
