#include <bits/stdc++.h>
using namespace std;
const int mn = 101000;
vector<int> e[mn];
int c[mn], s[mn], sc[mn], sv[mn];
int R[mn], vis[mn];
int n, m, i, j, k, nc, tt;
long long ans;
bool cmpc(int i, int j) { return c[i] > c[j]; }
bool cmps(int i, int j) { return s[i] < s[j]; }
void add(int i, int d, int l) {
  s[0] = l;
  int j = lower_bound(sv + 1, sv + 1 + n, 0, cmps) - sv;
  if (j == n + 1) return;
  j = sv[j];
  if (s[j] != l || c[j] > d) return;
  e[j].push_back(i);
}
inline bool dfs(int r) {
  int p, i;
  vis[r] = nc;
  for (p = 0; p < e[r].size(); ++p) {
    i = e[r][p];
    if (R[i] == 0 || (vis[R[i]] != nc && dfs(R[i]))) {
      R[i] = r;
      return 1;
    }
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> c[i] >> s[i];
  for (i = 1; i <= n; ++i) sc[i] = sv[i] = i;
  sort(sc + 1, sc + 1 + n, cmpc);
  sort(sv + 1, sv + 1 + n, cmps);
  cin >> m;
  for (i = 1; i <= m; ++i) {
    cin >> j >> k;
    add(i, j, k);
    add(i, j, k + 1);
  }
  for (i = 1; i <= n; ++i) {
    ++nc;
    if (dfs(sc[i])) ++tt, ans += c[sc[i]];
  }
  cout << ans << endl;
  cout << tt << endl;
  for (i = 1; i <= m; ++i)
    if (R[i]) cout << i << " " << R[i] << endl;
  return 0;
}
