#include <bits/stdc++.h>
using namespace std;
int ans, n, i, j, sum, num;
long long ne, k, st, x, y, mo;
struct sw {
  int x, y, z;
  bool operator<(const sw& s) const {
    return (x < s.x || (x == s.x && y < s.y) ||
            (x == s.x && y == s.y && z < s.z));
  }
} a[500000];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    sum = 0;
    cin >> k >> st >> x >> y >> mo;
    a[++num].x = sum;
    a[num].y = st;
    a[num].z = i;
    for (j = 2; j <= k; j++) {
      ne = (st * x + y) % mo;
      if (ne < st) sum++;
      a[++num].x = sum;
      a[num].y = ne;
      a[num].z = i;
      st = ne;
    }
    if (sum > ans) ans = sum;
  }
  sort(a + 1, a + num + 1);
  cout << ans << endl;
  if (num <= 200000) {
    for (i = 1; i <= num; i++) cout << a[i].y << ' ' << a[i].z << endl;
  }
}
