#include <bits/stdc++.h>
struct note {
  int x, y, z;
} a[200000 + 5];
int n, m, f, g, x, y, p, res, ans, tot;
bool cmp(const note i, const note j) {
  return i.x == j.x ? i.y < j.y : i.x < j.x;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d%d", &m, &f, &x, &y, &p), res = 0;
    for (int j = 1; j <= m; j++) {
      if (tot <= 200000) a[tot].x = res, a[tot].y = f, a[tot++].z = i;
      g = (1LL * f * x + y) % p;
      if (f > g && j < m) res++;
      f = g;
    }
    ans = ans > res ? ans : res;
  }
  printf("%d\n", ans);
  if (tot <= 200000) {
    std::sort(a, a + tot, cmp);
    for (int i = 0; i < tot; i++) printf("%d %d\n", a[i].y, a[i].z);
  }
  return 0;
}
