#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n, m;
  map<ll, ll> A;
  cin >> n;
  for (ll i = 0; i < n; i++) {
    ll a, b;
    cin >> a >> b;
    A[a] = b;
  }
  cin >> m;
  for (ll i = 0; i < m; i++) {
    ll a, b;
    cin >> a >> b;
    A[a] = max(A[a], b);
  }
  ll ans = 0;
  for (auto i : A) {
    ans += i.second;
  }
  cout << ans;
  return 0;
}
