#include <bits/stdc++.h>
using namespace std;
int n, m;
map<int, int> t;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = (1); i <= (n); ++i) {
    int a, x;
    cin >> a >> x;
    t[a] = x;
  }
  cin >> m;
  for (int i = (1); i <= (m); ++i) {
    int a, x;
    cin >> a >> x;
    t[a] = max(t[a], x);
  }
  long long ans = 0;
  for (pair<int, int> x : t) ans += x.second;
  cout << ans;
}
