#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005, MAXM = 200005;
int l[MAXN];
vector<int> a[MAXN];
int now[MAXN];
int ansn = 0;
int sk;
int n;
struct DATA {
  int num, src;
};
inline bool operator<(const DATA &a, const DATA &b) {
  if (a.num < b.num) return true;
  return a.num == b.num && a.src < b.src;
}
set<DATA> bst;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &l[i]);
    for (int j = 0; j <= l[i]; j++) a[i].push_back(0);
    scanf("%d", &a[i][1]);
    int x, y, m;
    scanf("%d%d%d", &x, &y, &m);
    for (int j = 2; j <= l[i]; j++)
      a[i][j] = (a[i][j - 1] * (long long)x + y) % m;
    sk += l[i];
  }
  for (int i = 1; i <= n; i++) {
    int t = 0;
    for (int j = 1; j < l[i]; j++)
      if (a[i][j] > a[i][j + 1]) t++;
    if (t > ansn) ansn = t;
  }
  printf("%d\n", ansn);
  if (sk > 200000) return 0;
  for (int i = 1; i <= n; i++) {
    DATA t;
    t.num = a[i][1];
    t.src = i;
    bst.insert(t);
  }
  int last = -1;
  for (int i = 1; i <= sk; i++) {
    DATA t;
    t.num = last;
    t.src = 0;
    set<DATA>::iterator p = bst.lower_bound(t);
    if (p == bst.end()) p = bst.begin();
    int best = p->src;
    last = p->num;
    bst.erase(p);
    printf("%d %d\n", last, best);
    now[best]++;
    if (now[best] < l[best]) {
      t.num = a[best][now[best] + 1];
      t.src = best;
      bst.insert(t);
    }
  }
  return 0;
}
