#include <bits/stdc++.h>
using namespace std;
struct mu {
  int flo, cost, id;
} ans[200005];
int n;
long long k, a, x, y, m, b, endd = 0, sum = -1, t;
bool cmp(mu u, mu v) {
  if (u.flo == v.flo)
    return (u.cost < v.cost || (u.cost == v.cost && u.id < v.id));
  return u.flo < v.flo;
}
int main() {
  ios::sync_with_stdio(false);
  int i = 0, j = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k >> a >> x >> y >> m;
    t = 0;
    for (j = 1; j <= k; j++) {
      if (endd <= 200000) {
        ans[endd].flo = t;
        ans[endd].cost = a;
        ans[endd++].id = i;
      }
      b = (a * x + y) % m;
      if (j != k && b < a) t++;
      a = b;
      sum = max(sum, t);
    }
  }
  cout << sum << endl;
  if (endd <= 200000) {
    sort(ans, ans + endd, cmp);
    for (i = 0; i < endd; i++) printf("%d %d\n", ans[i].cost, ans[i].id);
  }
  return 0;
}
