#include <bits/stdc++.h>
using namespace std;
inline bool smax(long long &a, long long b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
const long long MAXN = 1e5 + 100;
long long dp[MAXN][4], par[MAXN][4], choose[MAXN][4], id[MAXN];
pair<long long, long long> p[MAXN], s;
set<long long> sz;
map<long long, long long> cost, toid;
bool byP(long long x, long long y) {
  return greater<pair<long long, long long> >()(p[x], p[y]);
}
pair<long long, long long> ans[MAXN];
long long ansN;
void get(long long i, long long j) {
  if (i) get(i - 1, par[i][j]);
  if (choose[i][j] != -1)
    ans[ansN++] = pair<long long, long long>(i - 1, choose[i][j]);
}
int main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  memset(dp, -63, sizeof dp);
  memset(choose, -1, sizeof choose);
  long long m;
  cin >> m;
  for (long long i = 0; i < m; i++) {
    cin >> s.second >> s.first;
    sz.insert(s.first);
    cost[s.first] = s.second;
    toid[s.first] = i;
  }
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> p[i].second >> p[i].first, id[i] = i;
  p[n++] = pair<long long, long long>(-1, -1);
  id[n - 1] = n - 1;
  sort(id, id + n, byP);
  sort(p, p + n, greater<pair<long long, long long> >());
  long long tmp = 0;
  if (sz.count(p[0].first)) tmp += 1;
  if (sz.count(p[0].first + 1)) tmp += 2;
  dp[0][tmp] = 0;
  cerr << bitset<2>(tmp) << endl;
  cost[-1] = 0;
  for (long long i = 0; i < n - 1; i++)
    for (long long j = 0; j < 4; j++)
      if (dp[i][j] >= 0) {
        long long cur = p[i].first;
        vector<long long> t;
        t.push_back(-1);
        if (j & 1) t.push_back(cur);
        if (j >> 1 & 1) t.push_back(cur + 1);
        for (long long k = 0; k < t.size(); k++)
          if (p[i].second >= cost[t[k]]) {
            long long newmask = 0, next = p[i + 1].first;
            if (sz.count(next) && t[k] != next) newmask += 1;
            if (sz.count(next + 1) && t[k] != next + 1) newmask += 2;
            if (next == cur) newmask &= j;
            if (next == cur - 1 && j % 2 == 0) newmask -= newmask / 2 * 2;
            if (smax(dp[i + 1][newmask], dp[i][j] + cost[t[k]])) {
              par[i + 1][newmask] = j;
              choose[i + 1][newmask] = t[k];
            }
          }
      }
  get(n - 1, 0);
  cout << dp[n - 1][0] << "\n" << ansN << "\n";
  for (long long i = 0; i < ansN; i++)
    cout << id[ans[i].first] + 1 << " " << toid[ans[i].second] + 1 << "\n";
}
