#include <bits/stdc++.h>
using namespace std;
void solve() {
  unsigned long long int n, i, q = 0, c, d, e;
  cin >> n;
  unsigned long long int a, x;
  map<unsigned long long int, unsigned long long int> p;
  map<unsigned long long int, unsigned long long int>::iterator it;
  set<unsigned long long int> s;
  for (i = 0; i < n; i++) {
    cin >> a >> x;
    p[a] = x;
    s.insert(a);
    q += x;
  }
  c = s.size();
  unsigned long long int m;
  cin >> m;
  unsigned long long int b, y;
  for (i = 0; i < m; i++) {
    cin >> b >> y;
    s.insert(b);
    d = s.size();
    if (c == d) {
      e = p[b];
      q -= e;
      if (e >= y)
        q += e;
      else
        q += y;
    } else {
      q += y;
      c = d;
    }
  }
  cout << q << endl;
  return;
}
int main() { solve(); }
