#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> x(n);
  map<int, int> M1, M2;
  for (int i = 0; i < n; ++i) {
    int tmp1, tmp2;
    cin >> tmp1;
    a.push_back(tmp1);
    cin >> tmp2;
    x.push_back(tmp2);
    M1[tmp1] = tmp2;
  }
  int m;
  cin >> m;
  vector<int> b(m);
  vector<int> y(m);
  for (int i = 0; i < m; ++i) {
    int tmp1, tmp2;
    cin >> tmp1;
    b.push_back(tmp1);
    cin >> tmp2;
    y.push_back(tmp2);
    M2[tmp1] = tmp2;
  }
  auto total = 0ll;
  for (auto& x : M1) {
    auto it = M2.find(x.first);
    if (it == M2.end()) {
      total += x.second;
      continue;
    }
    if (x.second > it->second) {
      total += x.second;
    } else
      total += it->second;
  }
  for (auto& x : M2) {
    auto it = M1.find(x.first);
    if (it == M1.end()) {
      total += x.second;
      continue;
    }
  }
  cout << total << endl;
}
