#include <bits/stdc++.h>
using namespace std;
int main() {
  map<int, int> a, b;
  int idx, price;
  long long total = 0;
  int n, m;
  scanf(" %d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %d %d", &idx, &price);
    a[idx] = price;
    total += price;
  }
  scanf(" %d", &m);
  for (int i = 0; i < m; i++) {
    scanf(" %d %d", &idx, &price);
    b[idx] = price;
    if (a.count(idx) > 0 && a[idx] < price) {
      total += price;
      total -= a[idx];
    } else if (a.count(idx) == 0) {
      total += price;
    }
  }
  cout << total << endl;
}
