#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  map<long long, long long> m;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    if (m.count(a))
      m[a] = max(m[a], b);
    else
      m[a] = b;
  }
  int M;
  cin >> M;
  for (int i = 0; i < M; i++) {
    long long a, b;
    cin >> a >> b;
    if (m.count(a))
      m[a] = max(m[a], b);
    else
      m[a] = b;
  }
  long long sum = 0;
  for (auto u : m) {
    sum += u.second;
  }
  cout << sum << endl;
}
