#include <bits/stdc++.h>
using namespace std;
struct Boot {
  int num, size, price;
  Boot(){};
  Boot(int num, int size, int price) : num(num), size(size), price(price){};
};
bool IsLessSize(const Boot &boot1, const Boot &boot2) {
  return boot1.size < boot2.size;
}
bool IsGreaterPrice(const Boot &boot1, const Boot &boot2) {
  return boot1.price > boot2.price;
}
bool kuhn(int v, bool *used, int *mt, vector<int> *g) {
  if (used[v]) {
    return false;
  }
  used[v] = 1;
  for (auto &to : g[v]) {
    if (mt[to] == -1 || kuhn(mt[to], used, mt, g)) {
      mt[to] = v;
      return true;
    }
  }
  return false;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n;
  vector<Boot> boots;
  int prices[n];
  for (int i = 0; i < n; i++) {
    long long int price, size;
    cin >> price >> size;
    prices[i] = price;
    boots.emplace_back(i, size, price);
  }
  sort(boots.begin(), boots.end(), IsLessSize);
  cin >> m;
  vector<Boot> buyers;
  vector<int> g[n];
  for (int i = 0; i < m; i++) {
    long long int price, size;
    cin >> price >> size;
    buyers.emplace_back(i, size, price);
    auto lb =
        lower_bound(boots.begin(), boots.end(), Boot(-1, size, -1), IsLessSize);
    while (lb->size == size || (lb->size == size + 1)) {
      if (lb->price <= price) g[lb->num].emplace_back(i);
      lb++;
    }
  }
  sort(boots.begin(), boots.end(), IsGreaterPrice);
  bool used[n];
  int mt[m];
  memset(mt, -1, sizeof mt);
  for (int i = 0; i < n; i++) {
    memset(used, false, sizeof used);
    kuhn(boots[i].num, used, mt, g);
  }
  long long int sum = 0;
  int cnt = 0;
  for (int i = 0; i < m; i++) {
    if (mt[i] == -1) continue;
    sum += prices[mt[i]];
    cnt++;
  }
  cout << sum << endl;
  cout << cnt << endl;
  for (int i = 0; i < m; i++) {
    if (mt[i] == -1) continue;
    cout << (i + 1) << " " << (mt[i] + 1) << endl;
  }
  return 0;
}
