#include <bits/stdc++.h>
using namespace std;
long long n, sum = 0, max1 = -1, max2 = -1;
struct mm {
  long long id, p;
} a[100005], b[100005];
int cmp(mm a, mm b) { return a.id < b.id; }
int binarySearch(int x) {
  long left = 1;
  long right = n;
  while (left <= right) {
    int mid = (right + left) / 2;
    if (b[x].id == a[mid].id && b[x].p > a[mid].p)
      return b[x].p - a[mid].p;
    else if (b[x].id == a[mid].id && b[x].p <= a[mid].p)
      return 0;
    else if (a[mid].id > b[x].id)
      right = mid - 1;
    else if (a[mid].id < b[x].id)
      left = mid + 1;
  }
  return b[x].p;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].id >> a[i].p;
    sum += a[i].p;
  }
  sort(a + 1, a + 1 + n, cmp);
  int m;
  cin >> m;
  for (int i = 1; i <= m; i++) {
    cin >> b[i].id >> b[i].p;
    sum += binarySearch(i);
  }
  cout << sum << endl;
}
