#include <bits/stdc++.h>
using namespace std;
namespace splay_tree {
struct Node {
  Node *left, *right, *parent, *path_parent;
  int cnt;
  int key;
  Node() { left = right = parent = path_parent = nullptr; }
};
struct SplayTree {
  Node* root;
  SplayTree() { root = nullptr; }
  void Update(Node* n) {}
  void Preorder(Node* n) {
    cout << n->key << "\n";
    cout << "-LEFT" << n->key << "\n";
    if (n->left) Preorder(n->left);
    cout << "-RIGHT" << n->key << "\n";
    if (n->right) Preorder(n->right);
  }
  void LeftRotate(Node* n) {
    Node* nl = n->right;
    assert(nl != nullptr);
    n->right = nl->left;
    if (nl->left) nl->left->parent = n;
    nl->parent = n->parent;
    if (!n->parent)
      root = nl;
    else if (n == n->parent->left)
      n->parent->left = nl;
    else
      n->parent->right = nl;
    nl->left = n;
    n->parent = nl;
    nl->path_parent = n->path_parent;
    n->path_parent = nullptr;
    Update(nl);
    Update(n);
  }
  void RightRotate(Node* n) {
    Node* nl = n->left;
    assert(nl != nullptr);
    n->left = nl->right;
    if (nl->right) nl->right->parent = n;
    nl->parent = n->parent;
    if (!n->parent)
      root = nl;
    else if (n == n->parent->left)
      n->parent->left = nl;
    else
      n->parent->right = nl;
    nl->right = n;
    n->parent = nl;
    nl->path_parent = n->path_parent;
    n->path_parent = nullptr;
    Update(nl);
    Update(n);
  }
  void Splay(Node* n) {
    while (n->parent) {
      if (!n->parent->parent) {
        if (n->parent->left == n)
          RightRotate(n->parent);
        else
          LeftRotate(n->parent);
      } else if (n->parent->left == n && n->parent->parent->left == n->parent) {
        RightRotate(n->parent->parent);
        RightRotate(n->parent);
      } else if (n->parent->right == n &&
                 n->parent->parent->right == n->parent) {
        LeftRotate(n->parent->parent);
        LeftRotate(n->parent);
      } else if (n->parent->left == n &&
                 n->parent->parent->right == n->parent) {
        RightRotate(n->parent);
        LeftRotate(n->parent);
      } else {
        LeftRotate(n->parent);
        RightRotate(n->parent);
      }
    }
  }
  void Insert(int key) {
    Node* n = root;
    Node** nn;
    if (!n) {
      Node* x = new Node();
      root = x;
      x->key = key;
      return;
    }
    while (1) {
      if (key == n->key) return;
      if (key < n->key) {
        if (!n->left) {
          nn = &n->left;
          break;
        }
        n = n->left;
      } else {
        if (!n->right) {
          nn = &n->right;
          break;
        }
        n = n->right;
      }
    }
    Node* x = new Node();
    *nn = x;
    x->parent = n;
    x->key = key;
    Splay(x);
  }
  bool Find(int key) {
    Node* n = root;
    if (!n) return false;
    while (n) {
      if (key == n->key) break;
      if (key < n->key) {
        if (!n->left) break;
        n = n->left;
      } else {
        if (!n->right) break;
        n = n->right;
      }
    }
    Splay(n);
    return key == n->key;
  }
  void Erase(int key) {
    if (!Find(key)) return;
    Node* n = root;
    if (n->left) {
      if (n->right) {
        root = n->left;
        root->parent = nullptr;
        Node* x = root;
        while (x->right) x = x->right;
        x->right = n->right;
        n->right->parent = x;
        Splay(x);
        delete n;
        return;
      }
      root = n->left;
      root->parent = nullptr;
      delete n;
      return;
    }
    if (n->right) {
      root = n->right;
      root->parent = nullptr;
      delete n;
      return;
    }
    delete n;
    root = nullptr;
  }
  int FindKth(int k) {
    Node* n = root;
    assert(n->cnt >= k);
    while (1) {
      while (n->left && n->left->cnt > k) n = n->left;
      if (n->left) k -= n->left->cnt;
      if (!k--) break;
      n = n->right;
    }
    Splay(n);
    return n->key;
  }
  Node* Access(Node* n) {
    Splay(n);
    if (n->right) {
      n->right->path_parent = n;
      n->right->parent = nullptr;
      n->right = nullptr;
      Update(n);
    }
    Node* x = n;
    while (x->path_parent) {
      Node* y = x->path_parent;
      Splay(y);
      if (y->right) {
        y->right->path_parent = y;
        y->right->parent = nullptr;
      }
      y->right = x;
      x->parent = y;
      x->path_parent = nullptr;
      Update(y);
      Splay(x);
    }
    return x;
  }
  Node* FindRoot(Node* n) {
    Access(n);
    while (n->left) n = n->left;
    Splay(n);
    return n;
  }
  void Cut(Node* n) {
    Access(n);
    n->left->parent = nullptr;
    n->left = nullptr;
    Update(n);
  }
  void Link(Node* x, Node* y) {
    Access(x);
    Access(y);
    x->left = y;
    y->parent = x;
    Update(x);
  }
  Node* LCA(Node* x, Node* y) {
    Access(x);
    return Access(y);
  }
};
}  // namespace splay_tree
int Q, N, M, K, L;
splay_tree::SplayTree SET;
vector<pair<int, int> > A, B;
void __solve() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A.push_back({-b, a});
  }
  cin >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    A.push_back({-b, a});
  }
  sort(A.begin(), A.end());
  long long ret = 0;
  for (int i = 0; i < N + M; i++) {
    if (SET.Find(A[i].second)) continue;
    ret += (long long)-A[i].first;
    SET.Insert(A[i].second);
  }
  cout << ret;
}
int main(int argc, char** argv) {
  srand(time(NULL));
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  __solve();
  cin >> N;
}
