#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<long long> a[N];
int ptr[N];
int n;
int K[N];
long long x, y, m, k;
int main() {
  input(n);
  int sum = 0;
  set<pair<long long, int> > pq;
  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;
  }
  long long prv = -1;
  vector<pair<long long, int> > sq;
  long long 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;
    sq.push_back(*u);
    int now = u->second;
    pq.erase(u);
    if (ptr[now] < a[now].size())
      pq.insert(make_pair(a[now][ptr[now]], now)), ptr[now]++;
  }
  cout << ans << endl;
  if (sum <= 200000)
    for (auto a : sq) printf("%lld %d\n", a.first, a.second + 1);
  return 0;
}
