#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int inf = 0x3f3f3f3f;
inline long long read() {
  register long long x = 0, f = 1;
  register char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  return x * f;
}
void write(long long x) {
  if (x < 0) x = -x, putchar('-');
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  puts("");
}
const int maxn = 2e5;
pair<pair<int, int>, int> a[maxn];
int K, c, x, y, m, n, cnt, t, mx;
int main() {
  n = read();
  for (register int i = (1); i <= int(n); ++i) {
    int K = read(), c = read(), x = read(), y = read(), m = read();
    t = 0;
    for (register int j = (1); j <= int(K); ++j) {
      if (cnt <= maxn) a[++cnt] = make_pair(make_pair(t, c), i);
      if (j < K && c > (1ll * c * x + y) % m) t++;
      c = (1ll * c * x + y) % m;
    }
    mx = max(mx, t);
  }
  sort(a + 1, a + 1 + cnt);
  writeln(mx);
  if (cnt <= maxn)
    for (register int i = (1); i <= int(cnt); ++i)
      printf("%d %d\n", a[i].first.second, a[i].second);
  return 0;
}
