#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int n, m;
map<int, int> mp;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (mp.count(a))
      mp[a] = max(mp[a], b);
    else
      mp[a] = b;
  }
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (mp.count(a))
      mp[a] = max(mp[a], b);
    else
      mp[a] = b;
  }
  long long ans = 0;
  for (auto i : mp) ans += i.second;
  printf("%lld\n", ans);
  return 0;
}
