#include <bits/stdc++.h>
using namespace std;
struct node {
  int d, s;
  int id;
};
node a[100009], b[100009];
map<int, int> Map;
vector<int> e[100009], vec;
int match[100009];
int vis[100009];
int n, m;
bool cmp(node x, node y) { return x.d > y.d; }
bool find(int x) {
  for (int i = 0; i < e[x].size(); i++) {
    if (match[e[x][i]] == x) continue;
    if (match[e[x][i]] == 0 || find(match[e[x][i]])) {
      match[e[x][i]] = x;
      return true;
    }
  }
  return false;
}
int main() {
  int d, s;
  scanf("%d", &n);
  memset(match, 0, sizeof(match));
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].d, &a[i].s);
    a[i].id = i;
    Map[a[i].s] = i;
  }
  cin >> m;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &d, &s);
    if (Map.find(s) != Map.end()) {
      if (a[Map[s]].d <= d) e[Map[s]].push_back(i);
    }
    if (Map.find(s + 1) != Map.end()) {
      if (a[Map[s + 1]].d <= d) e[Map[s + 1]].push_back(i);
    }
  }
  sort(a + 1, a + 1 + n, cmp);
  long long ans1 = 0, ans2 = 0;
  for (int i = 1; i <= n; i++) {
    if (find(a[i].id)) {
      ans1++;
      ans2 += a[i].d;
    }
  }
  cout << ans2 << endl;
  cout << ans1 << endl;
  for (int i = 1; i <= m; i++) {
    if (match[i] != 0) cout << i << " " << match[i] << endl;
  }
  return 0;
}
