#include <bits/stdc++.h>
using namespace std;
map<int, int> g;
map<int, int>::iterator k;
long long ans;
int i, j, n, m, x, y, tot;
int a[1000001];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) cin >> x >> y, g[x] = y, a[++tot] = x;
  cin >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y;
    k = g.find(x);
    if (k != g.end())
      g[x] = max(g[x], y);
    else
      g[x] = y, a[++tot] = x;
  }
  for (i = 1; i <= tot; i++) ans += g[a[i]];
  cout << ans << endl;
  return 0;
  g[10] = 2;
  k = g.find(10);
  if (k != g.end()) g[10] = 9;
  cout << g[10];
  return 0;
}
