#include <bits/stdc++.h>
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 120000;
const double eps = 1e-9;
using namespace std;
pair<pair<int, int>, int> a[MAXN];
vector<int> adj[MAXN];
bool c1(pair<pair<int, int>, int> lhs, pair<pair<int, int>, int> rhs) {
  return lhs.first.second < rhs.first.second;
}
bool vis[MAXN];
int d[MAXN] = {0};
int dfs(int u) {
  for (int v : adj[u])
    if (!vis[v]) {
      vis[v] = true;
      if (!d[v] || dfs(d[v])) {
        d[v] = u;
        return true;
      }
    }
  return false;
}
int main() {
  int n, m;
  scanf("%d", &(n));
  for (int i = 0; i < n; i++) {
    scanf("%d", &(a[i].first.first)), scanf("%d", &(a[i].first.second));
    a[i].second = i + 1;
  }
  sort(a, a + n, c1);
  scanf("%d", &(m));
  for (int i = 0; i < m; i++) {
    int w, s;
    scanf("%d", &(w)), scanf("%d", &(s));
    for (int j = lower_bound(a, a + n, make_pair(make_pair(w, s), 0), c1) - a;
         j < n && (s == a[j].first.second || s == a[j].first.second - 1); j++)
      if (w >= a[j].first.first) adj[a[j].second].push_back(i + 1);
  }
  sort(
      a, a + n,
      [](pair<pair<int, int>, int> lhs, pair<pair<int, int>, int> rhs) -> bool {
        return lhs.first.first > rhs.first.first;
      });
  long long ans = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    memset(vis, false, sizeof vis);
    if (dfs(a[i].second)) {
      ans += a[i].first.first;
      cnt++;
    }
  }
  printf("%I64d\n%d\n", ans, cnt);
  for (int i = m; i > 0; i--)
    if (d[i]) printf("%d %d\n", i, d[i]);
  return 0;
}
