#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
struct node {
  int id;
  int p, sz;
} a[100005], b;
bool vis[100005];
int match[100005];
bool cmp1(node aa, node bb) { return aa.sz < bb.sz; }
bool cmp2(node aa, node bb) { return aa.p > bb.p; }
bool sear(int s) {
  int i, j;
  for (i = 0; i < v[s].size(); i++) {
    int t = v[s][i];
    if (!vis[t]) {
      vis[t] = 1;
      if (match[t] == 0 || sear(match[t])) {
        match[t] = s;
        return true;
      }
    }
  }
  return false;
}
int main() {
  int n, m;
  cin >> n;
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i].p >> a[i].sz;
    a[i].id = i + 1;
  }
  sort(a, a + n, cmp1);
  cin >> m;
  for (i = 1; i <= m; i++) {
    cin >> b.p >> b.sz;
    for (j = lower_bound(a, a + n, b, cmp1) - a;
         j < n && (a[j].sz - 1 == b.sz || a[j].sz == b.sz); j++) {
      if (b.p >= a[j].p) {
        v[a[j].id].push_back(i);
      }
    }
  }
  sort(a, a + n, cmp2);
  long long ans = 0;
  int cnt = 0;
  for (i = 0; i < n; i++) {
    memset(vis, 0, sizeof(vis));
    if (sear(a[i].id)) {
      ans += a[i].p;
      cnt++;
    }
  }
  cout << ans << endl << cnt << endl;
  for (i = 1; i <= m; i++) {
    if (match[i]) cout << i << " " << match[i] << endl;
  }
  return 0;
}
