#include <bits/stdc++.h>
using namespace std;
using namespace rel_ops;
const double PI = acos((double)-1);
int ts, ts2, ts3, ts4;
long long q2[35], n, m, ans, num2, x, y, K, a11;
long long gcd(long long x, long long y) {
  long long t;
  for (; y != 0;) {
    t = x % y;
    x = y;
    y = t;
  }
  return x;
}
double sqr(double x) { return x * x; }
template <class T>
void mn(T& x, T y) {
  x = min(x, y);
}
template <class T>
void mx(T& x, T y) {
  x = max(x, y);
}
vector<pair<int, int> > as[10005];
long long a[1000005];
void work() {
  long long i, j, l, t1, t2, t3, t4, t5, t6, t7, t8, t9, nw;
  for (i = 1; i <= n; i++) {
    scanf("%I64d%I64d%I64d%I64d%I64d", &K, &a11, &x, &y, &m);
    long long thisa = 0;
    long long nw = a11;
    for (j = 1; j <= K; j++) {
      if (num2 <= 200000) num2++, as[thisa].push_back(make_pair(nw, i));
      if (j == K) break;
      t2 = (nw * x + y) % m;
      a[j] = t2;
      if (t2 < nw) thisa++;
      nw = t2;
      if (x == 0) ts = 0;
    }
    thisa++;
    ans = max(ans, thisa);
  }
}
int main() {
  long long i, j, l, t1, t2, t3, t4, t5, t6, t7, t8, t9, nw;
  int tt1, tt2, tt3, tt4;
  double u1, u2, u3, u4, u5, u6, u7, u8, u9;
  char c1, c2, c3;
  for (q2[0] = 1, i = 1; i < 35; q2[i] = q2[i - 1] * 2, i++)
    ;
  srand((unsigned)time(0));
  cin >> n;
  work();
  printf("%I64d\n", ans - 1);
  if (num2 > 200000) return 0;
  for (i = 0; i <= ans; i++) {
    sort(as[i].begin(), as[i].end());
    for (j = 0; j < as[i].size(); j++)
      cout << as[i][j].first << " " << as[i][j].second << endl;
  }
  return 0;
}
