#include <bits/stdc++.h>
using namespace std;
int N, M;
int main() {
  scanf("%d", &N);
  map<int, int> m;
  for (int i = 1; i <= N; i++) {
    int a, x;
    scanf("%d %d", &a, &x);
    m[a] = x;
  }
  scanf("%d", &M);
  for (int i = 1; i <= M; i++) {
    int b, y;
    scanf("%d %d", &b, &y);
    m[b] = max(m[b], y);
  }
  long long res = 0;
  for (auto &entry : m) {
    res += entry.second;
  }
  cout << res << endl;
  return 0;
}
