#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, i, j, k, curr, c, id, total = 0, xx, xxx, cc;
  cin >> n;
  vector<long long> ct(n), v(n, 0), x(n), y(n), m(n), a(n);
  for (i = 0; i < n; i++) {
    cin >> ct[i];
    total += ct[i];
    cin >> a[i] >> x[i] >> y[i] >> m[i];
  }
  priority_queue<pair<long long, long long>> pq;
  queue<pair<long long, long long>> q;
  vector<pair<long long, long long>> ans;
  if (total <= 200000) {
    c = 0;
    curr = 0;
    for (i = 0; i < n; i++) pq.push({-a[i], i});
    while (!pq.empty() || !q.empty()) {
      if (pq.empty()) {
        c++;
        curr = 0;
        while (!q.empty()) {
          pq.push(q.front());
          q.pop();
        }
      }
      while (!pq.empty()) {
        xx = -pq.top().first;
        id = pq.top().second;
        pq.pop();
        if (total <= 200000) ans.push_back({xx, id});
        curr = xx;
        v[id]++;
        if (v[id] < ct[id]) {
          a[id] = (a[id] * x[id] + y[id]) % m[id];
          if (a[id] < curr)
            q.push({-a[id], id});
          else
            pq.push({-a[id], id});
        }
      }
    }
  } else {
    c = 0;
    for (i = 0; i < n; i++) {
      cc = 0;
      xx = a[i];
      for (j = 1; j < ct[i]; j++) {
        xxx = (xx * x[i] + y[i]) % m[i];
        if (xxx < xx) cc++;
        xx = xxx;
      }
      c = max(c, cc);
    }
  }
  cout << c << "\n";
  for (auto p : ans) cout << p.first << " " << p.second + 1 << "\n";
  return 0;
}
