#include <bits/stdc++.h>
using namespace std;
const int maxn = 100001;
struct arr {
  int x, y, z;
} a[maxn], b[maxn];
vector<int> q[maxn];
int f[maxn];
bool v[maxn];
int n, m, s, tot;
long long ans;
bool cmp(arr i, arr j) {
  if (i.y < j.y) return 1;
  return 0;
}
bool cmp2(arr i, arr j) {
  if (i.x > j.x) return 1;
  return 0;
}
bool find(int k) {
  int i, j;
  for (i = 0; i < q[k].size(); i++) {
    j = q[k][i];
    if (!v[j]) {
      v[j] = 1;
      if (f[j] == 0 || find(f[j])) {
        f[j] = k;
        return 1;
      }
    }
  }
  return 0;
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
    a[i].z = i + 1;
  }
  sort(a, a + n, cmp);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d%d", &b[i].x, &b[i].y);
    b[i].z = i + 1;
  }
  for (s = i = 0; i < m; i++)
    for (j = lower_bound(a, a + n, b[i], cmp) - a;
         b[i].y + 1 == a[j].y || b[i].y == a[j].y; j++)
      if (b[i].x >= a[j].x) q[a[j].z].push_back(b[i].z);
  sort(a, a + n, cmp2);
  for (i = 0; i < n; i++) {
    memset(v, 0, sizeof(v));
    if (find(a[i].z)) {
      ans += a[i].x;
      tot++;
    }
  }
  printf("%I64d\n", ans);
  printf("%d\n", tot);
  for (i = 1; i <= m; i++)
    if (f[i]) printf("%d %d\n", i, f[i]);
  return 0;
}
