#include <bits/stdc++.h>
using namespace std;
int n, nProblem;
struct problem {
  int who;
  int block;
  int costTime;
  long long info;
  problem() {}
  problem(int _w, int _b, int _c) { who = _w, block = _b, costTime = _c; }
  bool operator<(const problem &u) const {
    if (block != u.block) return block < u.block;
    return costTime < u.costTime;
  }
} P[200001];
int MAIN() {
  while (cin >> n) {
    int maxBlock = 0;
    nProblem = 0;
    for (int i = 1; i <= n; i++) {
      int k;
      long long a, x, y, m;
      cin >> k >> a >> x >> y >> m;
      long long prev = -100;
      int blockNumber = 1;
      for (int j = 1; j <= k; j++) {
        if (a < prev) blockNumber++;
        prev = a;
        if (nProblem + 1 <= 200000)
          P[++nProblem] = problem(i, blockNumber, a);
        else
          nProblem++;
        a = (a * x + y) % m;
      }
      maxBlock = max(maxBlock, blockNumber - 1);
    }
    if (nProblem <= 200000) sort(P + 1, P + 1 + nProblem);
    cout << maxBlock << endl;
    if (nProblem <= 200000) {
      for (int i = 1; i <= nProblem; i++)
        cout << P[i].costTime << " " << P[i].who << endl;
    }
  }
  return 0;
}
int main() {
  ios ::sync_with_stdio(false);
  cout << fixed << setprecision(16);
  int RUN_RESULT = MAIN();
  return RUN_RESULT;
}
