#include <bits/stdc++.h>
using namespace std;
template <class T>
void chmin(T& a, const T& b) {
  if (a > b) a = b;
}
template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}
tuple<long long, long long, long long> adj[400001];
long long cnt;
long long nm;
int main() {
  long long k, a, x, y, m, n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> k >> a >> x >> y >> m;
    long long t = 0;
    for (long long j = 1; j <= k; j++, adj[nm++] = make_tuple(t, a, i),
                   (a > ((a * x + y) % m) && j != k + 1) ? t++ : t,
                   a = (a * x + y) % m)
      ;
    cnt = max(cnt, t);
  }
  cout << cnt << "\n";
  sort(adj, adj + nm);
  for (long long i = 0; i < nm; i++)
    cout << get<1>(adj[i]) << " " << get<2>(adj[i]) << "\n";
}
