#include <bits/stdc++.h>
const long long N = 1e6 + 10;
const long long mod = 1e9 + 7;
long long dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
using namespace std;
map<long long, long long> mp;
set<long long> s;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long x, y;
    cin >> x >> y;
    s.insert(x);
    mp[x] = y;
  }
  long long m;
  cin >> m;
  for (long long i = 1; i <= m; i++) {
    long long x, y;
    cin >> x >> y;
    if (mp[x] == 0) {
      s.insert(x);
      mp[x] = y;
    } else
      mp[x] = max(mp[x], y);
  }
  long long ans = 0;
  for (auto &e : s) {
    ans += mp[e];
  }
  cout << ans << endl;
  return 0;
}
