#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, int> > v;
int n;
int main() {
  scanf("%d", &n);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int k, a, x, y, m;
    scanf("%d%d%d%d%d", &k, &a, &x, &y, &m);
    int p = 0, b = 0;
    for (int j = 1; j <= k; j++) {
      if (a < b) p++;
      if (v.size() <= 200000) v.push_back(make_pair(make_pair(p, a), i));
      b = a;
      a = ((long long)a * x + y) % m;
    }
    if (p > ans) ans = p;
  }
  printf("%d\n", ans);
  if (v.size() <= 200000) {
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
      printf("%d %d\n", v[i].first.second, v[i].second);
  }
  return 0;
}
