#include <bits/stdc++.h>
using namespace std;
map<int, int> arr;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  while (n--) {
    int a, x;
    cin >> a >> x;
    arr[a] = max(arr[a], x);
  }
  int m;
  cin >> m;
  while (m--) {
    int b, y;
    cin >> b >> y;
    arr[b] = max(arr[b], y);
  }
  map<int, int>::iterator it;
  long long ans = 0;
  for (it = arr.begin(); it != arr.end(); it++) {
    ans += it->second;
  }
  cout << ans;
  return 0;
}
