#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int N;
int c[2][MAXN];
int idx[2];
pair<int, int> p[MAXN];
int pn;
long long solve() {
  long long ret = 0;
  int pre = -1;
  for (int i = 0; i < N; ++i) idx[i] = 1;
  while (1) {
    int opt = 1000000007, x = -1;
    for (int i = 0; i < N; ++i) {
      if (idx[i] <= c[i][0]) {
        if (c[i][idx[i]] < opt && c[i][idx[i]] >= pre) {
          opt = c[i][idx[i]];
          x = i;
        }
      }
    }
    if (x != -1) {
      pre = opt;
      p[pn++] = make_pair(opt, x + 1);
      ++idx[x];
      continue;
    }
    opt = 1000000007, x = -1;
    for (int i = 0; i < N; ++i) {
      if (idx[i] <= c[i][0]) {
        if (c[i][idx[i]] < opt) {
          opt = c[i][idx[i]];
          x = i;
        }
      }
    }
    if (x == -1) return ret;
    ret += opt < pre;
    pre = opt;
    p[pn++] = make_pair(opt, x + 1);
    ++idx[x];
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> c[i][0] >> c[i][1];
    int x, y, m;
    cin >> x >> y >> m;
    for (int j = 2; j <= c[i][0]; ++j) {
      c[i][j] = (1LL * c[i][j - 1] * x + y) % m;
    }
  }
  cout << solve() << endl;
  for (int i = 0; i < pn; ++i) {
    printf("%d %d\n", p[i].first, p[i].second);
  }
  return 0;
}
