#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000;
const int LIMIT = 200000;
int ans, n;
struct Task {
  int when, where, who;
  friend bool operator<(Task a, Task b) {
    if (a.when != b.when) return a.when < b.when;
    if (a.where != b.where) return a.where < b.where;
    return a.who < b.who;
  }
};
vector<Task> ansSeq;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int k, fs, x, y, m;
    scanf("%d%d%d%d%d", &k, &fs, &x, &y, &m);
    int last = 0, curAns = 0;
    for (int j = 1; j <= k; j++) {
      if (fs < last) curAns++;
      last = fs;
      if (ansSeq.size() <= LIMIT) ansSeq.push_back((Task){curAns, fs, i});
      fs = (fs * (long long)x + y) % m;
    }
    ans = max(ans, curAns);
  }
  printf("%d\n", ans);
  if (ansSeq.size() <= LIMIT) {
    sort(ansSeq.begin(), ansSeq.end());
    for (int i = 0; i < ansSeq.size(); i++)
      printf("%d %d\n", ansSeq[i].where, ansSeq[i].who);
  }
  return 0;
}
