#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int m, u, t, n;
  long long count = 0;
  map<int, int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t >> u;
    v[t] = u;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> t >> u;
    if (v[t] < u) v[t] = u;
  }
  for (auto e : v) count += e.second;
  cout << count << endl;
  return 0;
}
