#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const long long LINF = 4000000000000000000LL;
const double eps = 1e-9;
void prepare() {}
const int maxn = 100100;
struct O {
  int c, s, id;
  bool operator<(const O &v) const { return c > v.c; }
};
int n, m;
O o[maxn];
vector<int> g[maxn];
map<int, int> ma;
int d[maxn], l[maxn];
int mt[maxn];
int used[maxn], T;
void read() {
  scanf("%d", &n);
  for (int i = (0); i < (((n))); i++) {
    scanf("%d%d", &o[i].c, &o[i].s);
    o[i].id = i;
  }
  scanf("%d", &m);
  for (int i = (0); i < (((m))); i++) scanf("%d%d", &d[i], &l[i]);
}
void adde(int ind, int cu) {
  if (d[cu] >= o[ind].c) g[ind].push_back(cu);
}
bool kuhn(int id) {
  if (used[id] == T) return false;
  used[id] = T;
  for (int i = (0); i < ((((int)(g[id]).size()))); i++) {
    int to = g[id][i];
    if (mt[to] < 0 || kuhn(mt[to])) {
      mt[to] = id;
      return true;
    }
  }
  return false;
}
void solve() {
  read();
  sort(o, o + n);
  for (int i = (0); i < (((n))); i++) ma[o[i].s] = i;
  for (int i = (0); i < (((m))); i++) {
    map<int, int>::iterator it = ma.find(l[i]);
    if (it != ma.end()) adde(it->second, i);
    it = ma.find(l[i] + 1);
    if (it != ma.end()) adde(it->second, i);
  }
  memset((mt), (-1), sizeof(mt));
  for (int i = (0); i < (((n))); i++) {
    T++;
    kuhn(i);
  }
  vector<pair<int, int> > res;
  long long sum = 0;
  for (int i = (0); i < (((m))); i++) {
    if (mt[i] >= 0) {
      res.push_back(make_pair(i, o[mt[i]].id));
      sum += o[mt[i]].c;
    }
  }
  printf("%I64d\n", sum);
  printf("%d\n", (int)(res).size());
  for (int i = (0); i < ((((int)(res).size()))); i++)
    printf("%d %d\n", res[i].first + 1, res[i].second + 1);
}
int main() {
  prepare();
  solve();
  return 0;
}
