#include <bits/stdc++.h>
using namespace std;
int ans, n, k, a, b, x, y, m, tot = 0;
struct data {
  int x, y, z;
  data() {}
  data(int _x, int _y, int _z) { x = _x, y = _y, z = _z; }
} A[200005];
int get() {
  int f = 0, v = 0;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') break;
  if (ch == '-')
    f = 1;
  else
    v = ch - 48;
  while (isdigit(ch = getchar())) v = v * 10 + ch - 48;
  if (f == 1) return -v;
  return v;
}
bool cmp(data a, data b) {
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
int main() {
  for (int n = get(), num = 1; n > 0; n--, num++) {
    k = get(), a = get(), x = get(), y = get(), m = get();
    int t = 1;
    b = 0;
    for (int i = 1; i <= k; i++) {
      if (b > a) t++;
      if (tot <= 200000) A[++tot] = data(t, a, num);
      b = a;
      a = ((long long)a * x + y) % m;
    }
    ans = max(ans, t);
  }
  printf("%d\n", ans - 1);
  if (tot <= 200000) {
    sort(A + 1, A + tot + 1, cmp);
    for (int i = 1; i <= tot; i++) printf("%d %d\n", A[i].y, A[i].z);
  }
  return 0;
}
