#include <bits/stdc++.h>
using namespace std;
const int maxN = 100010;
struct obj {
  int cost, size, id;
  obj(int c = -1, int sz = -1, int idx = -1) : cost(c), size(sz), id(idx) {}
};
bool compSize(obj a, obj b) { return a.size < b.size; }
bool compCost(obj a, obj b) { return a.cost > b.cost; }
vector<int> waitList[maxN];
int purchase[maxN];
bool sold[maxN];
bool buy(int x) {
  if (sold[x]) return false;
  sold[x] = true;
  for (int i = 0; i < waitList[x].size(); i++) {
    int pot = waitList[x][i];
    if (purchase[pot] == -1 || buy(purchase[pot])) {
      purchase[pot] = x;
      return true;
    }
  }
  return false;
}
obj shoes[maxN];
obj cus[maxN];
long long cost[maxN];
int main() {
  ios_base::sync_with_stdio(false);
  int N, M;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> shoes[i].cost >> shoes[i].size;
    shoes[i].id = i;
    cost[i] = shoes[i].cost;
  }
  sort(shoes, shoes + N, compSize);
  cin >> M;
  for (int i = 0; i < M; i++) {
    cin >> cus[i].cost >> cus[i].size;
    cus[i].id = i;
  }
  for (int i = 0; i < M; i++) {
    int idx =
        int(lower_bound(shoes, shoes + N, obj(-1, cus[i].size, -1), compSize) -
            shoes);
    while (shoes[idx].size == cus[i].size ||
           shoes[idx].size == (cus[i].size + 1)) {
      if (shoes[idx].cost <= cus[i].cost)
        waitList[shoes[idx].id].push_back(cus[i].id);
      idx++;
    }
  }
  sort(shoes, shoes + N, compCost);
  memset(purchase, -1, sizeof(purchase));
  for (int i = 0; i < N; i++) {
    memset(sold, false, sizeof(sold));
    buy(shoes[i].id);
  }
  long long total = 0LL;
  vector<pair<int, int> > ans;
  for (int i = 0; i < M; i++) {
    if (purchase[i] == -1) continue;
    total += cost[purchase[i]];
    ans.push_back(make_pair(i + 1, purchase[i] + 1));
  }
  cout << total << endl;
  cout << ans.size() << endl;
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << endl;
  return 0;
}
