#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
bitset<maxn> mark, fmatch;
vector<int> fv[maxn];
int n, m;
int match[maxn];
map<int, int> shoe;
vector<pair<int, int> > shoe2;
bool dfs(int p, int v) {
  if (mark[v]) return false;
  mark[v] = true;
  for (int u : fv[v]) {
    if (match[u] == -1 || dfs(v, match[u])) {
      match[u] = v;
      return true;
    }
  }
  return false;
}
void getinput() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    shoe[b] = i;
    shoe2.push_back(pair<int, int>({a, i}));
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    if (shoe.find(b + 1) != shoe.end() && shoe2[shoe[b + 1]].first <= a) {
      fv[shoe[b + 1]].push_back(i);
    }
    if (shoe.find(b) != shoe.end() && shoe2[shoe[b]].first <= a) {
      fv[shoe[b]].push_back(i);
    }
  }
}
int main() {
  memset(match, -1, sizeof(match));
  getinput();
  sort(shoe2.rbegin(), shoe2.rend());
  bool find = true;
  long long int cnt = 0, cost = 0;
  while (find) {
    find = 0;
    for (auto [x, y] : shoe2) {
      mark.reset();
      if (!fmatch[y] && dfs(0, y)) {
        fmatch[y] = true;
        find = true;
        cnt++;
        cost += x;
      }
    }
  }
  cout << cost << "\n" << cnt << "\n";
  for (int i = 0; i < m; i++) {
    if (match[i] != -1) {
      cout << i + 1 << " " << match[i] + 1 << "\n";
    }
  }
}
