#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
struct SHOES {
  int siz, cst, id;
} soe[N + 3];
bool cmpSHOES(const SHOES cpr1, const SHOES cpr2) {
  return cpr1.cst > cpr2.cst;
}
map<int, int> idx;
vector<int> lnk[2 * N + 3];
vector<pair<int, int> > ans;
int mat[2 * N + 3], vis[2 * N + 3];
int nsoe, ncos, vistag;
long long maxsel;
void AddEdge(int u, int v) {
  lnk[u].push_back(v);
  lnk[v].push_back(u);
}
bool DFS(int u) {
  vis[u] = vistag;
  for (auto v : lnk[u]) {
    if (vis[v] == vistag) continue;
    vis[v] = vistag;
    if (!mat[v] || DFS(mat[v])) {
      mat[u] = v;
      mat[v] = u;
      return true;
    }
  }
  return false;
}
void GetMatch() {
  for (int i = 1; i <= nsoe; i++)
    if (!mat[i]) {
      vistag++;
      DFS(i);
    }
}
int main() {
  scanf("%d", &nsoe);
  for (int i = 1; i <= nsoe; i++)
    scanf("%d%d", &soe[i].cst, &soe[i].siz), soe[i].id = i;
  sort(soe + 1, soe + 1 + nsoe, cmpSHOES);
  for (int i = 1; i <= nsoe; i++) idx[soe[i].siz] = i;
  scanf("%d", &ncos);
  for (int i = 1; i <= ncos; i++) {
    int siz, mny;
    scanf("%d%d", &mny, &siz);
    if (idx.count(siz))
      if (soe[idx[siz]].cst <= mny) AddEdge(idx[siz], i + nsoe);
    if (idx.count(siz + 1))
      if (soe[idx[siz + 1]].cst <= mny) AddEdge(idx[siz + 1], i + nsoe);
  }
  GetMatch();
  for (int i = 1; i <= nsoe; i++)
    if (mat[i])
      maxsel += soe[i].cst, ans.push_back(make_pair(mat[i] - nsoe, soe[i].id));
  printf("%lld\n%d\n", maxsel, ans.size());
  for (auto it : ans) printf("%d %d\n", it.first, it.second);
  return 0;
}
