#include <bits/stdc++.h>
using namespace std;
int n, to[100009 * 6], Next[100009 * 6], head[100009], match[100009];
int tot = 0;
int m, ret;
long long ans = 0;
vector<int> edge[100009];
struct cadongllas {
  int money, size, num;
} a[100009], b[100009];
void add(int u, int v) { edge[u].push_back(v); }
bool dfs(int x) {
  for (int i = 0; i < edge[x].size(); i++)
    if (match[edge[x][i]] != x) {
      if (match[edge[x][i]] == -1 || dfs(match[edge[x][i]])) {
        match[edge[x][i]] = x;
        return 1;
      }
    }
  return 0;
}
bool cmp(cadongllas x, cadongllas y) { return x.money > y.money; }
int main() {
  map<int, int> num;
  cin >> n;
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &a[i].money, &a[i].size), a[i].num = i;
  sort(a + 1, a + 1 + n, cmp);
  cin >> m;
  for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].money, &b[i].size);
  for (int i = 1; i <= n; i++) num[a[i].size] = i;
  for (int i = 1; i <= m; i++) {
    if (num[b[i].size + 1] && b[i].money >= a[num[b[i].size + 1]].money)
      add(num[b[i].size + 1], i);
    if (num[b[i].size] && b[i].money >= a[num[b[i].size]].money)
      add(num[b[i].size], i);
  }
  memset(match, -1, sizeof(match));
  for (int i = 1; i <= n; i++) {
    if (dfs(i)) {
      ans += a[i].money * 1LL;
      ret++;
    }
  }
  cout << ans << endl << ret << endl;
  for (int i = 1; i <= m; i++)
    if (match[i] != -1) printf("%d %d\n", i, a[match[i]].num);
  return 0;
}
