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