#include <bits/stdc++.h>
using namespace std;
class CompareDist {
 public:
  bool operator()(pair<long long int, long long int> n1,
                  pair<long long int, long long int> n2) {
    return n1.first > n2.first ||
           (n1.first == n2.first && n1.second < n2.second);
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) {
    long long int n;
    cin >> n;
    long long int arr[2];
    vector<pair<long long int, long long int> > v;
    for (int i = 0; i < n; i++) {
      cin >> arr[0] >> arr[1];
      v.push_back(make_pair(arr[0], arr[1]));
    }
    long long int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> arr[0] >> arr[1];
      v.push_back(make_pair(arr[0], arr[1]));
    }
    sort(v.begin(), v.end());
    long long int ans = 0;
    for (int i = 0; i < (m + n - 1); i++) {
      if (v[i].first != v[i + 1].first) {
        ans += v[i].second;
      } else if (v[i].first == v[i + 1].first) {
        ans += max(v[i].second, v[i + 1].second);
        ++i;
      }
    }
    if (v[n + m - 1].first != v[n + m - 2].first) ans += v[m + n - 1].second;
    cout << ans << "\n";
  }
}
