#include <bits/stdc++.h>
using namespace std;
int N;
int ki[(5005)];
vector<int> A[(5005)];
vector<int> sol;
set<pair<int, int> > S;
int main() {
  int i, j;
  scanf("%d", &N);
  int sumk = 0;
  for (i = 0; i < N; ++i) {
    int a, b, c, d, e;
    scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
    long long x, y, m;
    A[i].resize(a);
    ki[i] = a;
    sumk += a;
    A[i][0] = b;
    x = c;
    y = d;
    m = e;
    for (j = 1; j < a; ++j) A[i][j] = ((long long)A[i][j - 1] * x + y) % m;
  }
  int last = 0;
  int bad = 0;
  int sa = (sumk <= 200000);
  if (sa) {
    vector<int> cafe(N, 0);
    for (i = 0; i < N; ++i) S.insert(pair<int, int>(A[i][0], i));
    for (i = 0; i < sumk; ++i) {
      set<pair<int, int> >::iterator si =
          S.lower_bound(pair<int, int>(last, 0));
      if (si == S.end()) {
        ++bad;
        si = S.begin();
      }
      pair<int, int> p = *si;
      S.erase(si);
      int id = p.second;
      last = p.first;
      if (sa) sol.push_back(id);
      ++cafe[id];
      if (cafe[id] < ki[id]) S.insert(pair<int, int>(A[id][cafe[id]], id));
    }
    printf("%d\n", bad);
    cafe = vector<int>(N, 0);
    for (i = 0; i < sumk; ++i) {
      int id = sol[i];
      printf("%d %d\n", A[id][cafe[id]], id + 1);
      ++cafe[id];
    }
  } else {
    int maxb = 0;
    for (i = 0; i < N; ++i) {
      int b = 0;
      int sz = ki[i];
      for (j = 1; j < sz; ++j) {
        if (A[i][j - 1] > A[i][j]) ++b;
      }
      if (maxb < b) maxb = b;
    }
    printf("%d\n", maxb);
  }
  return 0;
}
