#include <bits/stdc++.h>
using namespace std;
struct shoes {
  int index;
  int cost;
  int size;
};
bool sortBySizeAcs(const shoes &a, const shoes &b) { return a.size < b.size; }
bool sortByCostDesc(const shoes &a, const shoes &b) { return a.cost > b.cost; }
int n, m;
shoes s[100000];
vector<int> edges[100000];
int pr[100000];
int vis[100000];
void add(int cus, int money, int size) {
  int pos = lower_bound(s, s + n, (shoes){-1, -1, size}, sortBySizeAcs) - s;
  if (pos == n) return;
  if (s[pos].size != size || s[pos].cost > money) return;
  edges[s[pos].index].push_back(cus);
}
bool dfs(int v, int flg) {
  vis[v] = flg;
  for (int i = 0; i < (int)(edges[v].size()); i++) {
    int w = edges[v][i];
    if (pr[w] == -1 || (vis[pr[w]] != flg && dfs(pr[w], flg))) {
      pr[w] = v;
      return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < (int)(n); i++) {
    cin >> s[i].cost >> s[i].size;
    s[i].index = i;
  }
  sort(s, s + n, sortBySizeAcs);
  cin >> m;
  for (int i = 0; i < (int)(m); i++) {
    int money, footsize;
    cin >> money >> footsize;
    add(i, money, footsize);
    add(i, money, footsize + 1);
  }
  long long ret = 0;
  int buy = 0;
  sort(s, s + n, sortByCostDesc);
  memset(pr, -1, sizeof(pr));
  for (int i = 0; i < (int)(n); i++) {
    if (dfs(s[i].index, i + 1)) {
      ret += s[i].cost;
      ++buy;
    }
  }
  cout << ret << endl;
  cout << buy << endl;
  for (int i = 0; i < (int)(m); i++)
    if (pr[i] != -1) cout << (i + 1) << " " << (pr[i] + 1) << endl;
  return 0;
}
