#include <bits/stdc++.h>
using namespace std;
mt19937 mt_rand(chrono::system_clock::now().time_since_epoch().count());
template <typename T1, typename T2>
inline bool upmax(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool upmin(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int maxn = (int)3e5 + 10;
const int base = 2147483647;
const long double PI = acos(-1.);
int n, m;
int main() {
  scanf("%d", &n);
  map<int, int> make_pair;
  for (int i = 1; i <= n; i++) {
    int a, x;
    scanf("%d%d", &a, &x);
    upmax(make_pair[a], x);
  }
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    int a, x;
    scanf("%d%d", &a, &x);
    upmax(make_pair[a], x);
  }
  long long ans = 0;
  for (auto& it : make_pair) {
    ans += 1LL * it.second;
  }
  cout << ans;
  return 0;
}
