#include <bits/stdc++.h>
using namespace std;
struct Node {
  int s, c;
  int id;
} shoe[100100];
struct P {
  int d, sz;
  int id;
} p[100100];
struct R {
  int a, b;
} Q[100100];
int n, m;
long long res = 0;
int k = 0;
int one[100100];
int two[100100][2];
bool cmp(Node a, Node b) { return a.s < b.s; }
bool cmp1(P a, P b) { return a.sz < b.sz; }
bool One(int l, int r) {
  int i, j;
  for (i = l; i <= r; i++) {
    if (one[i]) {
      for (j = l; j < i; j++) {
        Q[k].a = two[j + 1][0];
        Q[k++].b = shoe[j].id;
        res += shoe[j].c;
      }
      Q[k].a = one[i];
      Q[k++].b = shoe[i].id;
      res += shoe[i].c;
      for (j = i + 1; j <= r; j++) {
        Q[k].a = two[j][0];
        Q[k++].b = shoe[j].id;
        res += shoe[j].c;
      }
      return true;
    }
  }
  return false;
}
bool Two(int l, int r) {
  int i, j;
  for (i = l + 1; i <= r; i++) {
    if (two[i][0] && two[i][1]) {
      for (j = l; j < i - 1; j++) {
        Q[k].a = two[j + 1][0];
        Q[k++].b = shoe[j].id;
        res += shoe[j].c;
      }
      Q[k].a = two[i][0];
      Q[k++].b = shoe[i - 1].id;
      res += shoe[i - 1].c;
      Q[k].a = two[i][1];
      Q[k++].b = shoe[i].id;
      res += shoe[i].c;
      for (j = i + 1; j <= r; j++) {
        Q[k].a = two[j][0];
        Q[k++].b = shoe[j].id;
        res += shoe[j].c;
      }
      return true;
    }
  }
  return false;
}
int main() {
  while (scanf("%d", &n) != EOF) {
    int i, j;
    for (i = 0; i < n; i++) {
      scanf("%d%d", &shoe[i].c, &shoe[i].s);
      shoe[i].id = i + 1;
    }
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf("%d%d", &p[i].d, &p[i].sz);
      p[i].id = i + 1;
    }
    sort(shoe, shoe + n, cmp);
    sort(p, p + m, cmp1);
    res = 0, k = 0;
    memset(one, 0, sizeof(one));
    memset(two, 0, sizeof(two));
    for (i = 0, j = 0; i < n && j < m;) {
      if (shoe[i].s < p[j].sz) {
        i++;
      } else if (shoe[i].s == p[j].sz && i + 1 <= n &&
                 shoe[i + 1].s == p[j].sz + 1) {
        if (p[j].d >= shoe[i].c && p[j].d >= shoe[i + 1].c) {
          if (two[i + 1][0] == 0) {
            two[i + 1][0] = p[j].id;
          } else {
            two[i + 1][1] = p[j].id;
          }
        } else if (p[j].d >= shoe[i].c) {
          one[i] = p[j].id;
        } else if (p[j].d >= shoe[i + 1].c) {
          one[i + 1] = p[j].id;
        }
        j++;
      } else if (shoe[i].s == p[j].sz || shoe[i].s == p[j].sz + 1) {
        if (p[j].d >= shoe[i].c) {
          one[i] = p[j].id;
        }
        j++;
      } else
        j++;
    }
    i = 0;
    while (i < n) {
      int l, r;
      l = i;
      for (r = i + 1; r < n && two[r][0]; r++)
        ;
      r--;
      i = r + 1;
      if (l == r) {
        if (one[l]) {
          Q[k].a = one[l];
          Q[k++].b = shoe[l].id;
          res += shoe[l].c;
        }
      } else {
        if (!One(l, r)) {
          if (!Two(l, r)) {
            int mins = 1000000000 + 1, num;
            for (j = l; j <= r; j++) {
              if (mins > shoe[j].c) {
                mins = shoe[j].c;
                num = j;
              }
            }
            for (j = l; j < num; j++) {
              Q[k].a = two[j + 1][0];
              Q[k++].b = shoe[j].id;
              res += shoe[j].c;
            }
            for (j = num + 1; j <= r; j++) {
              Q[k].a = two[j][0];
              Q[k++].b = shoe[j].id;
              res += shoe[j].c;
            }
          }
        }
      }
    }
    cout << res << endl;
    cout << k << endl;
    for (i = 0; i < k; i++) {
      printf("%d %d\n", Q[i].a, Q[i].b);
    }
  }
  return 0;
}
