#include <bits/stdc++.h>
using namespace std;
const double DINF = numeric_limits<double>::infinity();
const long long MOD = 1e9 + 7;
const double EPS = 1e-7;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
mt19937 mmtw_(MOD);
uniform_int_distribution<long long> rd_;
long long randomll() { return rd_(mmtw_); }
long long rnd(long long x, long long y) { return rd_(mmtw_) % (y - x + 1) + x; }
template <class T>
T fact(T n) {
  if (n == 1) return 1;
  return n * fact(n - 1);
}
int n, m;
map<int, int> a;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int p, q;
    cin >> p >> q;
    a[p] = max(a[p], q);
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int p, q;
    cin >> p >> q;
    a[p] = max(a[p], q);
  }
  long long res = 0;
  for (auto it : a) {
    res += it.second;
  }
  cout << res << endl;
}
