#include <bits/stdc++.h>
using namespace std;
map<long long, long long> m1, m2;
vector<long long> v1, v2;
int main() {
  long long n, m, i, j, k, sum = 0, sum2 = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    v1.push_back(j);
    m1[j] = k;
  }
  cin >> m;
  for (i = 0; i < m; i++) {
    cin >> j >> k;
    v2.push_back(j);
    m2[j] = k;
  }
  for (i = 0; i < v1.size(); i++) {
    if (m2[v1[i]]) {
      if (m2[v1[i]] > m1[v1[i]]) {
        sum += m2[v1[i]];
        m2[v1[i]] = 0;
      } else {
        sum += m1[v1[i]];
        m2[v1[i]] = 0;
      }
    } else
      sum += m1[v1[i]];
  }
  for (i = 0; i < v2.size(); i++) {
    sum += m2[v2[i]];
  }
  cout << sum;
  return 0;
}
