#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
inline int read() {
  register int c = getchar(), x = 0, f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x * f;
}
const int MAXN = 1e5 + 7;
vector<int> vec;
map<int, int> mp;
bool vis[MAXN * 2];
int n, m;
unsigned long long ans = 0;
int main(void) {
  n = read();
  for (int i = 1; i <= n; ++i) {
    int x = read(), y = read();
    mp[x] = y;
    vec.push_back(x);
  }
  m = read();
  for (int i = 1; i <= m; ++i) {
    int x = read(), y = read();
    if (!mp[x])
      mp[x] = y;
    else
      mp[x] = max(mp[x], y);
    vec.push_back(x);
  }
  sort(vec.begin(), vec.end());
  auto it2 = unique(vec.begin(), vec.end());
  for (auto it = vec.begin(); it != it2; ++it) {
    ans += mp[*it];
  }
  cout << ans;
  return 0;
}
