#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
void amin(T& a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
void amax(T& a, U b) {
  if (a < b) a = b;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
void solve() {
  long long n, k;
  cin >> n;
  map<long long, long long> m;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    m[x] = max(m[x], y);
  }
  cin >> k;
  for (long long i = 0; i < k; i++) {
    long long x, y;
    cin >> x >> y;
    m[x] = max(m[x], y);
  }
  long long ans = 0;
  for (auto i : m) {
    ans += i.second;
  }
  cout << ans << "\n";
}
void input() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  input();
  int i = 1;
  { solve(); }
}
