#include <bits/stdc++.h>
using namespace std;
const double PI = 2.0 * acos(0.0);
const double EPS = 1e-6;
map<int, int> ss;
vector<pair<int, int> > shoe;
vector<pair<int, int> > greed;
vector<int> e[100005];
int match[100005];
bool berge(int x) {
  for (__typeof((e[x]).begin()) itr = (e[x]).begin(); itr != (e[x]).end();
       itr++) {
    if (match[*itr] == x) continue;
    if (match[*itr] == -1 || berge(match[*itr])) {
      match[*itr] = x;
      return true;
    }
  }
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int c, s;
    scanf("%d %d", &c, &s);
    shoe.push_back(make_pair(c, s));
    ss.insert(make_pair(s, i));
    greed.push_back(make_pair(c, i));
  }
  sort(greed.begin(), greed.end(), greater<pair<int, int> >());
  int m;
  scanf("%d", &m);
  for (int j = 0; j < m; j++) {
    int d, l;
    scanf("%d %d", &d, &l);
    if (ss.find(l) != ss.end() && shoe[ss[l]].first <= d)
      e[ss[l]].push_back(j + 1);
    if (ss.find(l + 1) != ss.end() && shoe[ss[l + 1]].first <= d)
      e[ss[l + 1]].push_back(j + 1);
  }
  memset(match, -1, sizeof(match));
  long long profit = 0LL;
  int sell = 0;
  for (int k = 0; k < n; k++) {
    if (berge(greed[k].second)) {
      profit += greed[k].first;
      sell++;
    }
  }
  printf("%I64d\n", profit);
  printf("%d\n", sell);
  for (int j = 1; j <= m; j++)
    if (match[j] != -1) printf("%d %d\n", j, match[j] + 1);
  return 0;
}
