#include <bits/stdc++.h>
using namespace std;
long long a[200009];
map<long long, long long> mp;
map<long long, long long> mp1;
map<long long, long long> mp2;
vector<string> vc;
struct ss {
  long long a, b, c;
  string s;
};
bool acompare(ss lhs, ss rhs) { return lhs.a < rhs.a; }
ss bbb[200002];
int main() {
  cin.sync_with_stdio(false);
  long long i, j, k, l, c, v, b1, n, m, r, t, e, f, h, d, N, p, y;
  long long x1, x2, y1, y2, x3, x4, x5, x6;
  string ss[100000];
  while (cin >> n) {
    c = 0;
    for (i = 0; i < n; i++) {
      cin >> j >> v;
      mp[j] = v;
      c += v;
    }
    cin >> m;
    for (i = 0; i < m; i++) {
      cin >> j >> v;
      if (mp[j] > 0) {
        if (mp[j] < v) {
          c -= mp[j];
          c += v;
        }
      } else
        c += v;
    }
    cout << c;
  }
}
