#include <bits/stdc++.h>
using namespace std;
long long ans;
int x, y, n, m;
map<int, int> ma;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> x >> y, ma[x] = y, ans += y;
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    if (ma[x] < y) {
      ans -= ma[x];
      ans += y;
    }
  }
  cout << ans;
  return 0;
}
