#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n;
  map<int, int> a;
  vector<int> p;
  for (int i = 0; i < n; i++) {
    int x;
    int y;
    cin >> x >> y;
    if (!a[x]) {
      p.push_back(x);
      a[x] = y;
    } else {
      if (a[x] < y) a[x] = y;
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    if (a[x] < y) {
      if (!a[x]) p.push_back(x);
      a[x] = y;
    }
  }
  long long int t = 0;
  for (int i = 0; i < p.size(); i++) t += a[p[i]];
  cout << t << endl;
  return 0;
}
