#include <bits/stdc++.h>
using namespace std;
template <class T>
void input(T &x) {
  char c = getchar();
  x = 0;
  for (; (c < 48 || c > 57); c = getchar())
    ;
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
}
inline long long bigmod(long long p, long long e, long long M) {
  long long ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
const int N = 5005;
vector<int> a[N];
int ptr[N];
int n;
int K[N];
long long x, y, m, k;
set<pair<int, int> > pq;
vector<pair<int, int> > sq;
int main() {
  input(n);
  int sum = 0;
  for (int i = 0; i < n; i++) {
    input(k);
    a[i].resize(k);
    input(a[i][0]);
    input(x), input(y), input(m);
    for (int j = 1; j < k; j++) {
      a[i][j] = (x * a[i][j - 1] + y) % m;
    }
    K[i] = k;
    sum += k;
    pq.insert(make_pair(a[i][0], i));
    ptr[i] = 1;
  }
  if (sum <= 200000) {
    int prv = -1;
    int ans = 0;
    while (pq.size()) {
      auto u = pq.lower_bound(make_pair(prv, -1));
      if (u == pq.end()) {
        ans++;
        u = pq.begin();
      }
      prv = u->first;
      if (sum <= 200000) sq.push_back(*u);
      int now = u->second;
      pq.erase(u);
      if (ptr[now] < K[now])
        pq.insert(make_pair(a[now][ptr[now]], now)), ptr[now]++;
    }
    cout << ans << endl;
    for (auto a : sq) printf("%d %d\n", a.first, a.second + 1);
  } else {
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int cnt = 0;
      for (int j = 1; j < K[i]; j++) cnt += (a[i][j] < a[i][j - 1]);
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
  return 0;
}
