#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 1e2;
struct edge {
  int v, x, y, next;
} e[maxn], a[maxn];
int size = 0, cnt = 0, head[maxn] = {0};
void insert(int t1, int t2, int t3, int t4) {
  if (cnt > 200000) return;
  e[++cnt].v = t4;
  e[cnt].next = head[t1];
  e[cnt].x = t2;
  e[cnt].y = t3;
  head[t1] = cnt;
}
int getint() {
  char ch;
  int b = 1;
  for (ch = getchar(); ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') b = -1;
  int num = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    num = num * 10 + ch - '0';
  return num * b;
}
bool cmp(edge t1, edge t2) { return t1.v < t2.v; }
int main() {
  int n = getint();
  int ans = 0, all = 0;
  for (int i = 1; i <= n; i++) {
    int t = getint();
    all += t;
    long long now = getint();
    int x = getint(), y = getint(), m = getint();
    insert(1, i, 1, now);
    int flag = 1;
    for (int j = 2; j <= t; j++) {
      long long tt = (now * x + y) % m;
      if (tt < now) {
        flag++;
      }
      now = tt;
      if (flag <= 200000) insert(flag, i, j, now);
    }
    ans = max(ans, flag);
  }
  if (all <= 200000) {
    int last = 0;
    for (int i = 1; i <= ans; i++) {
      for (int j = head[i]; j; j = e[j].next) {
        a[++size] = e[j];
      }
      sort(a + last + 1, a + size + 1, cmp);
      last = size;
    }
  }
  ans--;
  printf("%d\n", ans);
  if (all <= 200000) {
    for (int i = 1; i <= size; i++) {
      printf("%d %d\n", a[i].v, a[i].x);
    }
  }
  return 0;
}
