#include <bits/stdc++.h>
using namespace std;
int n, ma;
map<int, long long> c;
map<int, int> m;
struct cust {
  int l;
  long long d;
  long long in;
};
long long dp[110000][5];
cust com[110000];
int a, b;
long long dprec(int in, int state) {
  long long &ret = dp[in][state];
  if (ret != -1) return ret;
  long long d1, d2;
  d1 = c[com[in].l];
  d2 = c[com[in].l + 1];
  ret = 0;
  if (ma == in) {
    if (state % 2 && com[in].d >= d2) ret = d2;
    if (state / 2 && com[in].d >= d1) ret = max(ret, d1);
    return ret;
  }
  int stat;
  long long tem;
  if ((state % 2) && com[in].d >= d2 && d2) {
    tem = d2;
    if (com[in + 1].l == com[in].l)
      tem += dprec(in + 1, state ^ 1);
    else if (com[in + 1].l == com[in].l - 1)
      tem += dprec(in + 1, 2 + (state / 2));
    else
      tem += dprec(in + 1, 3);
    ret = tem;
  }
  if (state / 2 && com[in].d >= d1 && d1) {
    tem = d1;
    if (com[in + 1].l == com[in].l)
      tem += dprec(in + 1, state ^ 2);
    else if (com[in + 1].l == com[in].l - 1)
      tem += dprec(in + 1, 2);
    else
      tem += dprec(in + 1, 3);
    ret = max(ret, tem);
  }
  if (com[in + 1].l == com[in].l)
    tem = dprec(in + 1, state);
  else if (com[in + 1].l == com[in].l - 1)
    tem = dprec(in + 1, 2 + state / 2);
  else
    tem = dprec(in + 1, 3);
  ret = max(ret, tem);
  return ret;
}
vector<int> ans1, ans2;
bool path(int in, int state) {
  long long ret = dp[in][state];
  long long d1, d2;
  d1 = c[com[in].l];
  d2 = c[com[in].l + 1];
  if (ma == in) {
    if (state % 2 && com[in].d >= d2 && ret == d2 && ret) {
      ans1.push_back(com[in].in);
      ans2.push_back(m[com[in].l + 1]);
    } else if (state / 2 && com[in].d >= d1 && ret == d1 && ret) {
      ans1.push_back(com[in].in);
      ans2.push_back(m[com[in].l]);
    }
    return false;
  }
  int stat;
  long long tem;
  a = in + 1;
  if (state % 2 && com[in].d >= d2 && d2) {
    tem = d2;
    if (com[in + 1].l == com[in].l)
      tem += dprec(in + 1, state ^ 1);
    else if (com[in + 1].l == com[in].l - 1)
      tem += dprec(in + 1, 2 + (state / 2));
    else
      tem += dprec(in + 1, 3);
    if (ret == tem) {
      if (com[in + 1].l == com[in].l)
        b = state ^ 1;
      else if (com[in + 1].l == com[in].l - 1)
        b = 2 + (state / 2);
      else
        b = 3;
      ans1.push_back(com[in].in);
      ans2.push_back(m[com[in].l + 1]);
      return true;
    }
  }
  if (state / 2 && com[in].d >= d1 && d1) {
    tem = d1;
    if (com[in + 1].l == com[in].l)
      tem += dprec(in + 1, state ^ 2);
    else if (com[in + 1].l == com[in].l - 1)
      tem += dprec(in + 1, 2);
    else
      tem += dprec(in + 1, 3);
    if (ret == tem) {
      if (com[in + 1].l == com[in].l)
        b = state ^ 2;
      else if (com[in + 1].l == com[in].l - 1)
        b = 2;
      else
        b = 3;
      ans1.push_back(com[in].in);
      ans2.push_back(m[com[in].l]);
      return true;
    }
  }
  if (com[in + 1].l == com[in].l)
    b = state;
  else if (com[in + 1].l == com[in].l - 1)
    b = 2 + state / 2;
  else
    b = 3;
  return true;
}
bool comp(cust p, cust q) { return p.l > q.l; }
int main() {
  while (cin >> n) {
    int i, j;
    for (i = 1; i <= n; i++) {
      cin >> a >> b;
      c[b] = a;
      m[b] = i;
    }
    cin >> ma;
    for (i = 1; i <= ma; i++) {
      cin >> com[i].d >> com[i].l;
      com[i].in = i;
    }
    sort(&com[1], &com[ma + 1], comp);
    memset(dp, -1, sizeof(dp));
    for (i = ma; i >= 1; i--)
      for (j = 0; j <= 3; j++) dprec(i, j);
    cout << dprec(1, 3) << endl;
    a = 1;
    b = 3;
    while (path(a, b))
      ;
    int lim = (int)ans1.size();
    cout << lim << endl;
    for (i = 0; i <= lim - 1; i++) cout << ans1[i] << " " << ans2[i] << endl;
  }
  return 0;
}
