#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n;
long long k, a, x, y, m;
long long ans, tot = 0, t, b;
struct sc {
  long long x;
  long long y;
  long long z;
} p[N];
bool cmp(sc x, sc y) {
  if (x.x == y.x) {
    return (x.y < y.y || (x.y == y.y && x.z < y.z));
  } else {
    return x.x < y.x;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> k >> a >> x >> y >> m;
    t = 0;
    for (int j = 1; j <= k; ++j) {
      if (tot <= 200000) {
        sc s;
        s.x = t;
        s.y = a;
        s.z = i;
        p[++tot] = s;
      }
      b = (a * x + y) % m;
      if (b < a && j != k) ++t;
      a = b;
      ans = max(ans, t);
    }
  }
  cout << ans << "\n";
  if (tot <= 200000) {
    sort(p + 1, p + 1 + tot, cmp);
    for (int i = 1; i <= tot; ++i) {
      cout << p[i].y << " " << p[i].z << endl;
    }
  }
  return 0;
}
