#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long int MXN = 1e18;
map<int, int> mp;
int main() {
  int n, m, a, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> x;
    mp[a] = x;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> x;
    if (mp[a] < x) mp[a] = x;
  }
  long long int ans = 0;
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
    ans += it->second;
  }
  cout << ans << endl;
}
