#include <bits/stdc++.h>
using namespace std;
struct shoeShelf {
  int shoeCost;
  int shoeSize;
  int index;
};
bool compare(const shoeShelf &firstShoeShelf,
             const shoeShelf &secondShoeShelf) {
  return firstShoeShelf.shoeSize < secondShoeShelf.shoeSize;
}
bool compare1(const shoeShelf &firstShoeShelf,
              const shoeShelf &secondShoeShelf) {
  return firstShoeShelf.shoeCost > secondShoeShelf.shoeCost;
}
bool kuhn(int i, vector<int> &matching, vector<int> &buyers,
          vector<vector<int> > &graph) {
  for (vector<int>::iterator it = graph[i].begin(); it != graph[i].end();
       it++) {
    if (matching[*it] == i) continue;
    if (matching[*it] == -1 || kuhn(matching[*it], matching, buyers, graph)) {
      matching[*it] = i;
      buyers[i] = *it;
      return 1;
    }
  }
  return 0;
}
int main() {
  int n, m;
  cin >> n;
  vector<shoeShelf> allShoes(n);
  for (int i = 0; i < n; i++) {
    cin >> allShoes[i].shoeCost >> allShoes[i].shoeSize;
    allShoes[i].index = i;
  }
  sort(allShoes.begin(), allShoes.end(), compare);
  vector<vector<int> > graph(n);
  cin >> m;
  for (int i = 0; i < m; i++) {
    shoeShelf currentBuyer;
    cin >> currentBuyer.shoeCost >> currentBuyer.shoeSize;
    vector<shoeShelf>::iterator it;
    it = lower_bound(allShoes.begin(), allShoes.end(), currentBuyer, compare);
    if (it->shoeSize == currentBuyer.shoeSize &&
        it->shoeCost <= currentBuyer.shoeCost)
      graph[it->index].push_back(i);
    currentBuyer.shoeSize++;
    it = lower_bound(allShoes.begin(), allShoes.end(), currentBuyer, compare);
    if (it->shoeSize == currentBuyer.shoeSize &&
        it->shoeCost <= currentBuyer.shoeCost)
      graph[it->index].push_back(i);
  }
  sort(allShoes.begin(), allShoes.end(), compare1);
  vector<int> matching(m, -1), buyer(n, -1);
  int totalBuyers = 0;
  long long totalSum = 0;
  for (int i = 0; i < n; i++)
    if (kuhn(allShoes[i].index, matching, buyer, graph)) {
      totalSum += allShoes[i].shoeCost;
      totalBuyers++;
    }
  cout << totalSum << "\n" << totalBuyers << "\n";
  for (int i = 0; i < n; i++)
    if (buyer[i] != -1) cout << buyer[i] + 1 << " " << i + 1 << "\n";
}
