#include <bits/stdc++.h>
using namespace std;
struct node {
  int c, s, p;
} s[120000], r[120000];
vector<int> lin[120000];
int now;
int used[120000], match[120000];
int cmp1(node a, node b) { return a.s < b.s; }
int cmp2(node a, node b) { return a.c > b.c; }
bool dfs(int t) {
  for (int i = 0; i < lin[t].size(); i++) {
    int j = lin[t][i];
    if (used[j] != now) {
      used[j] = now;
      if (match[j] == 0 || dfs(match[j])) {
        match[j] = t;
        return true;
      }
    }
  }
  return false;
}
int main() {
  long long n, m;
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++)
    scanf("%I64d%I64d", &s[i].c, &s[i].s), s[i].p = i;
  ;
  sort(s + 1, s + 1 + n, cmp1);
  scanf("%I64d", &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%I64d%I64d", &r[i].c, &r[i].s);
    long long d = lower_bound(s + 1, s + n + 1, r[i], cmp1) - s;
    if (s[d].s - r[i].s == 0 || s[d].s - r[i].s == 1)
      if (s[d].c <= r[i].c) lin[s[d].p].push_back(i);
    if (s[d + 1].s - r[i].s == 0 || s[d + 1].s - r[i].s == 1)
      if (s[d + 1].c <= r[i].c) lin[s[d + 1].p].push_back(i);
  }
  sort(s + 1, s + 1 + n, cmp2);
  memset(used, 0, sizeof(used));
  memset(match, 0, sizeof(match));
  long long ans = 0, cou = 0;
  for (int i = 1; i <= n; i++) {
    now = i;
    if (dfs(s[i].p)) {
      ans += s[i].c;
      cou++;
    }
  }
  printf("%I64d\n%I64d\n", ans, cou);
  for (long long i = 1; i <= m; i++)
    if (match[i] != 0) printf("%I64d %I64d\n", i, match[i]);
  return 0;
}
