#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > s;
int l[5010], r[5010], a[5010][5010], b[2][200010];
void solve() {
  int sum = 0, i, j, m;
  for (i = 0; i < 2; i++) {
    int first, second;
    scanf("%d%d%d%d%d", &l[i], &b[i][1], &first, &second, &m);
    for (j = 2; j <= l[i]; j++)
      b[i][j] = ((long long)b[i][j - 1] * first % m + second) % m;
    sum += l[i];
  }
  int res = 0;
  for (i = 0; i < 2; i++) {
    m = 0;
    for (j = 2; j <= l[i]; j++)
      if (b[i][j] < b[i][j - 1]) m++;
    res = max(res, m);
  }
  printf("%d\n", res);
  if (sum > 200000) return;
  s.clear();
  for (i = 0; i < 2; i++) s.insert(make_pair(b[i][1], i)), r[i] = 1;
  set<pair<int, int> >::iterator it;
  int p = 0;
  while (!s.empty()) {
    it = s.lower_bound(make_pair(p, -1));
    if (it == s.end()) it = s.begin();
    s.erase(*it);
    int first = it->first, second = it->second;
    printf("%d %d\n", first, second + 1);
    sum++;
    p = first;
    if (r[second] == l[second]) continue;
    r[second]++;
    s.insert(make_pair(b[second][r[second]], second));
  }
}
int main() {
  int i, j, k, n, m;
  while (~scanf("%d", &n)) {
    int sum = 0;
    if (n == 2) {
      solve();
      continue;
    }
    for (i = 0; i < n; i++) {
      int b, first, second;
      scanf("%d%d%d%d%d", &l[i], &b, &first, &second, &m);
      a[i][1] = b;
      for (j = 2; j <= l[i]; j++)
        a[i][j] = ((long long)a[i][j - 1] * first % m + second) % m;
      sum += l[i];
    }
    int res = 0;
    for (i = 0; i < n; i++) {
      m = 0;
      for (j = 2; j <= l[i]; j++)
        if (a[i][j] < a[i][j - 1]) m++;
      res = max(res, m);
    }
    printf("%d\n", res);
    if (sum > 200000) continue;
    s.clear();
    for (i = 0; i < n; i++) s.insert(make_pair(a[i][1], i)), r[i] = 1;
    set<pair<int, int> >::iterator it;
    int p = 0;
    while (!s.empty()) {
      it = s.lower_bound(make_pair(p, -1));
      if (it == s.end()) it = s.begin();
      s.erase(*it);
      int first = it->first, second = it->second;
      printf("%d %d\n", first, second + 1);
      sum++;
      p = first;
      if (r[second] == l[second]) continue;
      r[second]++;
      s.insert(make_pair(a[second][r[second]], second));
    }
  }
  return 0;
}
