#include <bits/stdc++.h>
using namespace std;
int64_t t, n, m, k, a, b, ans = 0;
string s;
vector<pair<int64_t, int64_t> > p;
map<int64_t, bool> mp;
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    p.push_back(make_pair(a, b));
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    p.push_back(make_pair(a, b));
  }
  sort(p.begin(), p.end());
  bool flag = 0;
  for (int i = p.size() - 1; i >= 0; i--) {
    if (mp[p[i].first] == 0) {
      mp[p[i].first] = 1;
      ans += p[i].second;
    }
  }
  cout << ans;
}
