#include <bits/stdc++.h>
const int maxn = 2e5;
using namespace std;
map<int, int> a;
map<int, int>::iterator it;
pair<int, int> one;
int main() {
  int n, m, i, x, y;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    one.first = x;
    one.second = y;
    a.insert(one);
  }
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &x, &y);
    if (a[x] < y) a[x] = y;
  }
  long long ans = 0;
  while (!a.empty()) {
    it = a.begin();
    one = *it;
    ans = ans + one.second;
    a.erase(it);
  }
  cout << ans << endl;
}
