#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m;
struct node {
  int pos;
  int val;
};
node a[N], b[N];
bool cmp(node a, node b) { return a.pos < b.pos; }
int bs(int k) {
  int l = 1, r = n, mid = (l + r) / 2;
  ;
  while (mid != l && mid != r) {
    if (a[mid].pos <= k)
      l = mid;
    else
      r = mid;
    mid = (l + r) / 2;
  }
  for (int i = l; i <= r; ++i)
    if (a[i].pos == k) return i;
  return -1;
}
int main() {
  int i, j;
  long long sum = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i].pos >> a[i].val;
    sum += a[i].val;
  }
  sort(a + 1, a + n + 1, cmp);
  cin >> m;
  for (i = 1; i <= m; i++) {
    cin >> b[i].pos >> b[i].val;
    if (bs(b[i].pos) == -1)
      sum += b[i].val;
    else {
      if (a[bs(b[i].pos)].val < b[i].val)
        sum = sum + b[i].val - a[bs(b[i].pos)].val;
    }
  }
  cout << sum;
}
