#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  map<long long, long long> m;
  for (int i = 0; i < n; ++i) {
    long long x, y;
    cin >> x >> y;
    m.insert(pair<long long, long long>(x, y));
  }
  int p;
  cin >> p;
  for (int i = 0; i < p; ++i) {
    long long x, y;
    cin >> x >> y;
    if (m.find(x) != m.end()) {
      m[x] = max(m[x], y);
    } else {
      m.insert(pair<long long, long long>(x, y));
    }
  }
  long long ans = 0;
  for (auto i : m) {
    ans += i.second;
  }
  cout << ans;
}
