#include <bits/stdc++.h>
using namespace std;
const int MaxSize = 200010;
int n, m;
vector<pair<int, int> > V[MaxSize];
int main() {
  int K, a, x, y, P, t, i, j, c, ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%d%d%d%d%d", &K, &a, &x, &y, &P);
    if (c = 0, m < MaxSize) V[0].push_back(pair<int, int>(a, i)), ++m;
    while (--K) {
      if (t = ((long long)a * x + y) % P, t < a) ++c;
      if (a = t, m < MaxSize) V[c].push_back(pair<int, int>(a, i)), ++m;
    }
    ans = max(ans, c);
  }
  printf("%d\n", ans);
  if (m > 200000) return 0;
  for (i = 0; i <= ans; ++i) {
    vector<pair<int, int> >& V = ::V[i];
    sort(V.begin(), V.end());
    for (j = 0; j < V.size(); ++j) printf("%d %d\n", V[j].first, V[j].second);
  }
  return 0;
}
