#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 10;
struct T {
  long long cost, x, id;
  T(){};
  T(long long _c, long long _x, long long _i) : cost(_c), x(_x), id(_i){};
};
bool cmp(T a, T b) { return a.x == b.x ? a.cost < b.cost : a.x < b.x; }
const long long N = 1e5 + 10;
long long dp[N][4];
pair<long long, long long> from[N][4];
signed main() {
  ios_base::sync_with_stdio(0);
  long long n;
  cin >> n;
  vector<T> have;
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    have.push_back(T(a, b, i + 1));
  }
  sort((have).begin(), (have).end(), cmp);
  long long m;
  cin >> m;
  vector<T> arr;
  for (long long i = 0; i < m; i++) {
    long long a, b;
    cin >> a >> b;
    arr.push_back(T(a, b, i + 1));
  }
  sort((arr).begin(), (arr).end(), cmp);
  long long ptr = 0, ptr2 = 0;
  for (long long i = 1; i <= m; i++) {
    while (ptr < n && have[ptr].x < arr[i - 1].x) {
      ptr++;
    }
    while (ptr2 < n && have[ptr2].x <= arr[i - 1].x) {
      ptr2++;
    }
    if (i == 1) {
      dp[i][0] = 0;
      dp[i][3] = 0;
      if (ptr < n && have[ptr].x == arr[i - 1].x &&
          arr[i - 1].cost >= have[ptr].cost) {
        dp[i][1] = have[ptr].cost;
        from[i][1] = {0, 1};
      }
      if (ptr2 < n && have[ptr2].x == arr[i - 1].x + 1 &&
          arr[i - 1].cost >= have[ptr2].cost) {
        dp[i][2] = have[ptr2].cost;
        from[i][2] = {0, 2};
      }
      continue;
    }
    long long cur = arr[i - 2].x;
    if (cur == arr[i - 1].x) {
      for (long long cc = 0; cc < 4; cc++) {
        dp[i][cc] = dp[i - 1][cc];
        from[i][cc] = {cc, 0};
      }
    } else if (cur + 1 == arr[i - 1].x) {
      for (long long cc = 0; cc < 2; cc++) {
        if (dp[i - 1][cc] > dp[i][0]) {
          dp[i][0] = dp[i - 1][cc];
          from[i][0] = {cc, 0};
        }
      }
      for (long long cc = 2; cc < 4; cc++) {
        if (dp[i - 1][cc] > dp[i][1]) {
          dp[i][1] = dp[i - 1][cc];
          from[i][1] = {cc, 0};
        }
      }
    } else {
      for (long long cc = 0; cc < 4; cc++) {
        if (dp[i - 1][cc] > dp[i][0]) {
          dp[i][0] = dp[i - 1][cc];
          from[i][0] = {cc, 0};
        }
      }
    }
    for (long long cc = 0; cc < 4; cc++) {
      for (long long fr = 0; fr < 4; fr++) {
        if (cc == 1 && cur + min(2ll, fr) - 1 < arr[i - 1].x && ptr < n &&
            have[ptr].x == arr[i - 1].x && arr[i - 1].cost >= have[ptr].cost) {
          if (dp[i - 1][fr] + have[ptr].cost > dp[i][cc]) {
            dp[i][cc] = dp[i - 1][fr] + have[ptr].cost;
            from[i][cc] = {fr, 1};
          }
        }
        if (cc == 2 && cur + min(2ll, fr) - 1 < arr[i - 1].x && ptr2 < n &&
            have[ptr2].x == arr[i - 1].x + 1 &&
            arr[i - 1].cost >= have[ptr2].cost) {
          if (dp[i - 1][fr] + have[ptr2].cost > dp[i][cc]) {
            dp[i][cc] = dp[i - 1][fr] + have[ptr2].cost;
            from[i][cc] = {fr, 2};
          }
        }
        if (cc == 3) {
          if (cur == arr[i - 1].x - 1) {
            if (fr >= 2 && ptr2 < n && have[ptr2].x == arr[i - 1].x + 1 &&
                arr[i - 1].cost >= have[ptr2].cost) {
              if (dp[i - 1][fr] + have[ptr2].cost > dp[i][cc]) {
                dp[i][cc] = dp[i - 1][fr] + have[ptr2].cost;
                from[i][cc] = {fr, 2};
              }
            }
          }
          if (cur == arr[i - 1].x) {
            if (fr == 1 && ptr2 < n && have[ptr2].x == arr[i - 1].x + 1 &&
                arr[i - 1].cost >= have[ptr2].cost) {
              if (dp[i - 1][fr] + have[ptr2].cost > dp[i][cc]) {
                dp[i][cc] = dp[i - 1][fr] + have[ptr2].cost;
                from[i][cc] = {fr, 2};
              }
            }
            if (fr == 2 && ptr < n && have[ptr].x == arr[i - 1].x &&
                arr[i - 1].cost >= have[ptr].cost) {
              if (dp[i - 1][fr] + have[ptr].cost > dp[i][cc]) {
                dp[i][cc] = dp[i - 1][fr] + have[ptr].cost;
                from[i][cc] = {fr, 1};
              }
            }
          }
        }
      }
    }
  }
  pair<long long, long long> best = {0, 0};
  for (long long i = 0; i < 4; i++) {
    best = max(best, {dp[m][i], i});
  }
  cout << best.first << '\n';
  ptr = ptr2 = n - 1;
  vector<pair<long long, long long>> ans;
  for (long long i = m; i >= 1; i--) {
    pair<long long, long long> kek = from[i][best.second];
    while (ptr > 0 && have[ptr].x > arr[i - 1].x) {
      ptr--;
    }
    while (ptr2 > 0 && have[ptr2].x > arr[i - 1].x + 1) {
      ptr2--;
    }
    if (kek.second == 2) {
      ans.push_back({arr[i - 1].id, have[ptr2].id});
    } else if (kek.second == 1) {
      ans.push_back({arr[i - 1].id, have[ptr].id});
    }
    best.second = kek.first;
  }
  cout << (long long)(ans).size() << '\n';
  for (auto it : ans) {
    cout << it.first << ' ' << it.second << '\n';
  }
}
