#include <bits/stdc++.h>
using namespace std;
struct node {
  int ct, sz, id;
};
node nd1[100009], nd2[100009];
bool cmp1(node a1, node b1) { return a1.ct > b1.ct; }
map<int, int> Map;
vector<int> a[100009];
int match[100009], vis[100009], n, m;
bool find(int x) {
  for (int i = 0; i < a[x].size(); i++) {
    if (match[a[x][i]] == x) continue;
    if (match[a[x][i]] == 0 || find(match[a[x][i]])) {
      match[a[x][i]] = x;
      return true;
    }
  }
  return false;
}
int main() {
  int ct, sz, i;
  long long sum = 0, ans = 0;
  memset(match, 0, sizeof(match));
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &ct, &sz);
    nd1[i].sz = sz, nd1[i].ct = ct, nd1[i].id = i;
    Map[sz] = i;
  }
  scanf("%d", &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &ct, &sz);
    if (Map.find(sz) != Map.end()) {
      if (nd1[Map[sz]].ct <= ct) a[Map[sz]].push_back(i);
    }
    if (Map.find(sz + 1) != Map.end()) {
      if (nd1[Map[sz + 1]].ct <= ct) a[Map[sz + 1]].push_back(i);
    }
  }
  sort(nd1 + 1, nd1 + n + 1, cmp1);
  for (i = 1; i <= n; i++) {
    if (find(nd1[i].id)) {
      ans += nd1[i].ct, sum++;
    }
  }
  cout << ans << "\n" << sum << endl;
  for (i = 1; i <= m; i++) {
    if (match[i] != 0) printf("%d %d\n", i, match[i]);
  }
  return 0;
}
