#include <bits/stdc++.h>
using namespace std;
void input() {
  freopen("input.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
int binarySearch(long long arr[], int l, int r, int x) {
  while (l <= r) {
    int m = l + (r - l) / 2;
    if (arr[m] == x) return m;
    if (arr[m] < x)
      l = m + 1;
    else
      r = m - 1;
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  long long sum = 0;
  unordered_map<int, int> mp;
  vector<pair<int, int>> vec;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    vec.push_back(make_pair(x, y));
    sum += y;
  }
  sort(vec.begin(), vec.end());
  long long f[n], s[n];
  for (int i = 0; i < n; i++) {
    f[i] = vec[i].first;
    s[i] = vec[i].second;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    int res = binarySearch(f, 0, n, x);
    if (res != -1) {
      if (s[res] < y) {
        sum -= s[res];
        sum += y;
      }
    } else
      sum += y;
  }
  cout << sum << endl;
}
