#include <bits/stdc++.h>
using namespace std;
map<int, int> maxn;
int n, m;
int indice, income;
long long ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> indice >> income;
    maxn[indice] = income;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> indice >> income;
    ;
    maxn[indice] = max(maxn[indice], income);
  }
  for (auto it = maxn.begin(); it != maxn.end(); it++) {
    ans += it->second;
  }
  cout << ans;
}
