#include <bits/stdc++.h>
using namespace std;
map<int, int> a;
struct point {
  int m, s, n, w[100];
} b[100005], c[100005];
vector<int> d[100000];
int fa[100005];
bool sml(point a, point b) { return a.m > b.m; }
bool can(int now) {
  vector<int>::iterator i;
  for (i = d[now].begin(); i < d[now].end(); i++) {
    if (fa[*i] == now) continue;
    if (fa[*i] == -1 || can(fa[*i])) {
      fa[*i] = now;
      return true;
    }
  }
  return false;
}
int main() {
  int n, m;
  scanf("%d", &n);
  for (int i = 0; i != n; i++) {
    scanf("%d%d", &b[i].m, &b[i].s);
    b[i].n = i + 1;
  }
  sort(b, b + n, sml);
  for (int i = 0; i != n; i++) a[b[i].s] = i;
  scanf("%d", &m);
  for (int i = 0; i != m; i++) {
    scanf("%d%d", &c[i].m, &c[i].s);
    if (a.count(c[i].s) != 0 && c[i].m >= b[a[c[i].s]].m)
      d[a[c[i].s]].push_back(i);
    if (a.count(c[i].s + 1) != 0 && c[i].m >= b[a[c[i].s + 1]].m)
      d[a[c[i].s + 1]].push_back(i);
  }
  long long ans = 0;
  int k = 0;
  for (int i = 0; i != m; i++) fa[i] = -1;
  for (int i = 0; i != n; i++)
    if (can(i)) {
      ans += b[i].m;
      k++;
    }
  printf("%I64d\n%d\n", ans, k);
  for (int i = 0; i != m; i++)
    if (fa[i] != -1) {
      printf("%d %d\n", i + 1, b[fa[i]].n);
    }
}
