#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000 * 1000 + 1;
const long long INF = 1e18;
const int MOD = 1000 * 1000 * 1000 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, mx;
  map<long long, long long> m;
  map<long long, long long>::iterator it;
  cin >> n;
  long long x, y;
  for (long long i = 0; i < n; i++) {
    cin >> x >> y;
    m.insert(make_pair(x, y));
  }
  cin >> mx;
  for (long long i = 0; i < mx; i++) {
    cin >> x >> y;
    if (m.find(x) != m.end())
      m[x] = (m[x] < y ? y : m[x]);
    else
      m.insert(make_pair(x, y));
  }
  long long sum = 0;
  for (it = m.begin(); it != m.end(); it++) sum += it->second;
  cout << sum;
}
