#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ll n, m;
  cin >> n;
  map<ll, ll> cf;
  ll t_cf = 0;
  while (n--) {
    ll a, x;
    cin >> a >> x;
    cf[a] = x;
    t_cf += x;
  }
  cin >> m;
  ll t_tc = 0;
  while (m--) {
    ll b, y;
    cin >> b >> y;
    if (cf[b] < y && cf[b] != 0) {
      t_cf -= cf[b];
      t_tc += y;
    }
    if (cf[b] == 0) {
      t_tc += y;
    }
  }
  cout << t_tc + t_cf << endl;
  return 0;
}
