#include <bits/stdc++.h>
using namespace std;
const int MAXK = 200000;
pair<int, pair<int, int> > lis[MAXK + 10];
int mxkcnt, lcnt;
int main() {
  int n, a, k, x, y, m;
  mxkcnt = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int kcnt = 0;
    scanf("%d%d%d%d%d", &k, &a, &x, &y, &m);
    ++kcnt;
    if (lcnt <= 200000) lis[++lcnt] = make_pair(1, make_pair(a, i));
    for (int j = 2; j <= k; j++) {
      int nexa = (1ll * a * x + y) % m;
      if (nexa < a) ++kcnt;
      a = nexa;
      if (lcnt <= 200000) lis[++lcnt] = make_pair(kcnt, make_pair(a, i));
    }
    mxkcnt = max(mxkcnt, kcnt);
  }
  sort(lis + 1, lis + 1 + lcnt);
  printf("%d\n", mxkcnt - 1);
  if (lcnt <= 200000) {
    for (int i = 1; i <= lcnt; i++)
      printf("%d %d\n", lis[i].second.first, lis[i].second.second);
  }
  return 0;
}
