#include <bits/stdc++.h>
using namespace std;
void _add(long long &a, long long b) { a = (a + b) % 1000000007; }
void _sub(long long &a, long long b) { a = (a + 1000000007 - b) % 1000000007; }
void _mul(long long &a, long long b) { a = (long long)a * b % 1000000007; }
void _max(int &a, int b) { a = max(a, b); }
void _min(int &a, int b) { a = min(a, b); }
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long pow(long long a, long long b) {
  long long res(1);
  while (b) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return res;
}
long long powM(long long a, long long b, long long mod) {
  long long res(1);
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
const int infi = 2147483647;
const long long infl = 9223372036854775807ll;
const double PI = 3.14159265358979323846;
int n, m, k, T;
map<int, int> h;
int main() {
  scanf("%d", &n);
  for (int i = 1, lim = n; i <= lim; ++i) {
    int x, y;
    scanf("%d", &x), scanf("%d", &y);
    h[x] = y;
  }
  scanf("%d", &m);
  for (int i = 1, lim = m; i <= lim; ++i) {
    int x, y;
    scanf("%d", &x), scanf("%d", &y);
    if (h.count(x))
      h[x] = max(h[x], y);
    else
      h[x] = y;
  }
  long long ans = 0;
  for (auto p : h) {
    ans += p.second;
  }
  printf("%I64d", (ans));
  return 0;
}
