#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  unordered_map<int, int> price;
  for (int i = 0; i < n; i++) {
    int a, x;
    cin >> a >> x;
    price[a] = x;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int b, y;
    cin >> b >> y;
    if (price.count(b) > 0)
      price[b] = max(price[b], y);
    else
      price[b] = y;
  }
  long long income = 0;
  for (auto i : price) {
    income += i.second;
  }
  cout << income;
  return 0;
}
