#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int inf = int(2e18);
const int _inf = -inf;
const int mod = 998244353;
bool used[100001];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    a[x] = y;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    a[x] = max(a[x], y);
  }
  map<int, int>::iterator it;
  long long sum = 0;
  for (it = a.begin(); it != a.end(); ++it) {
    sum += it->second;
  }
  cout << sum;
  return 0;
}
