#include <bits/stdc++.h>
using namespace std;
struct triple {
  int a, b, c;
  bool operator<(const triple &r) const {
    return a != r.a ? a < r.a : b != r.b ? b < r.b : c < r.c;
  }
};
vector<triple> V;
int main() {
  int n, res(0);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int k, c(0), val, x, y, m, nval;
    scanf("%d%d%d%d%d", &k, &val, &x, &y, &m);
    for (int j = 1; j <= k; ++j) {
      if (V.size() <= 200000) V.push_back((triple){c, val, i});
      nval = ((long long)val * x + y) % m;
      if (nval < val && j != k) ++c;
      val = nval;
    }
    res = max(res, c);
  }
  printf("%d\n", res);
  if (V.size() <= 200000) {
    sort(V.begin(), V.end());
    for (auto it = V.begin(); it != V.end(); ++it)
      printf("%d %d\n", it->b, it->c);
  }
}
