#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr1[100000];
pair<int, int> arr2[100000];
int main() {
  cin.sync_with_stdio(false), cin.tie(nullptr);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr1[i].first >> arr1[i].second;
  int m;
  cin >> m;
  for (int j = 0; j < m; j++) cin >> arr2[j].first >> arr2[j].second;
  sort(arr1, arr1 + n);
  sort(arr2, arr2 + m);
  int i = 0;
  int j = 0;
  long long ans = 0;
  while (i < n && j < m) {
    if (arr1[i].first > arr2[j].first)
      ans += arr2[j++].second;
    else if (arr1[i].first < arr2[j].first)
      ans += arr1[i++].second;
    else {
      if (arr1[i].second > arr2[j].second)
        ans += arr1[i].second;
      else
        ans += arr2[j].second;
      i++, j++;
    }
  }
  while (i < n) ans += arr1[i++].second;
  while (j < m) ans += arr2[j++].second;
  cout << ans << '\n';
  return 0;
}
