#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int n, k, a, x, y, m, cnt, ans;
struct job {
  int rev, a, i;
} T[maxn];
inline bool operator<(job a, job b) {
  return (a.rev < b.rev || (a.rev == b.rev && a.i != b.i && a.a < b.a));
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int cur = 0, pre = -1;
    for (scanf("%d%d%d%d%d", &k, &a, &x, &y, &m); k--;) {
      if (cnt < maxn) {
        T[cnt].rev = cur;
        T[cnt].a = a;
        T[cnt++].i = i;
      } else
        cnt++;
      pre = a;
      a = ((long long)a * x + y) % m;
      if (pre > a && k) cur++;
    }
    ans = max(ans, cur);
  }
  printf("%d\n", ans);
  if (cnt <= maxn) {
    stable_sort(T, T + cnt);
    for (int i = 0; i < cnt; i++) printf("%d %d\n", T[i].a, T[i].i);
  }
}
