#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m;
struct shoe {
  shoe() {}
  shoe(long long c, long long s, int num) : c(c), s(s), num(num) {}
  long long c, s;
  int num;
};
bool compC(const shoe& a, const shoe& b) { return a.c > b.c; }
bool compS(const shoe& a, const shoe& b) { return a.s < b.s; }
struct customer {
  long long d, l;
  int num;
};
shoe shoes[maxn];
customer customers[maxn];
vector<int> g[maxn];
bool used[maxn];
int buy[maxn];
long long cost[maxn];
int mt[maxn];
bool tr(int v) {
  if (used[v]) return false;
  used[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (mt[to] == -1 || tr(mt[to])) {
      mt[to] = v;
      return true;
    }
  }
  return false;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &shoes[i].c, &shoes[i].s);
    shoes[i].num = i;
    cost[i] = shoes[i].c;
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &customers[i].d, &customers[i].l);
    customers[i].num = i;
  }
  sort(shoes, shoes + n, compS);
  for (int i = 0; i < m; i++) {
    int idx =
        lower_bound(shoes, shoes + n, shoe(-1, customers[i].l, -1), compS) -
        shoes;
    while (shoes[idx].s == customers[i].l ||
           shoes[idx].s == customers[i].l + 1) {
      if (shoes[idx].c <= customers[i].d) {
        g[shoes[idx].num].push_back(customers[i].num);
      }
      idx++;
    }
  }
  sort(shoes, shoes + n, compC);
  memset(mt, -1, sizeof(buy));
  for (int i = 0; i < n; i++) {
    memset(used, false, sizeof(used));
    tr(shoes[i].num);
  }
  vector<pair<int, int> > result;
  long long sum = 0;
  for (int i = 0; i < m; i++) {
    if (mt[i] != -1) {
      result.push_back({i, mt[i]});
      sum += cost[mt[i]];
    }
  }
  cout << sum << endl;
  cout << result.size() << endl;
  for (int i = 0; i < result.size(); i++) {
    cout << result[i].first + 1 << ' ' << result[i].second + 1 << endl;
  }
  return 0;
}
