#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const long long int INFLL = 0x3f3f3f3f3f3f3f3fll;
long long int getint() {
  long long int ret = 0;
  bool f = 0;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f ^= 1;
  while (c >= '0' && c <= '9') {
    ret = ret * 10 + c - '0';
    c = getchar();
  }
  return f ? -ret : ret;
}
using namespace std;
const int MAXN = 5000;
int len[MAXN + 10], a[MAXN + 10], x[MAXN + 10], y[MAXN + 10], mod[MAXN + 10];
struct cmp {
  bool operator()(int i, int j) const {
    return a[i] == a[j] ? i < j : a[i] < a[j];
  }
};
set<int, cmp> st;
int main() {
  int n = getint(), sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += len[i] = getint();
    a[i] = getint(), x[i] = getint();
    y[i] = getint(), mod[i] = getint();
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int lst = a[i], num = 0;
    for (int j = 2; j <= len[i]; ++j) {
      int t = ((long long int)lst * x[i] + y[i]) % mod[i];
      num += (t < lst);
      lst = t;
    }
    ans = max(ans, num);
  }
  printf("%d\n", ans);
  if (sum > 200000) return 0;
  a[0] = -INF;
  for (int i = 1; i <= n; ++i) st.insert(i);
  while (sum--) {
    set<int, cmp>::iterator it = st.lower_bound(0);
    int p;
    if (it != st.end())
      p = *it;
    else
      p = *st.begin();
    a[0] = a[p];
    printf("%d %d\n", a[p], p);
    st.erase(p);
    --len[p];
    if (!len[p]) continue;
    a[p] = ((long long int)a[p] * x[p] + y[p]) % mod[p];
    st.insert(p);
  }
}
