#include <bits/stdc++.h>
using namespace std;
int n, len[5005], a[5005], x[5005], y[5005], mod[5005], c[5005], sumlen;
int pt[200005][2], cnt, ans;
struct cmp {
  bool operator()(const int &A, const int &B) {
    if (c[A] != c[B]) return c[A] < c[B];
    return A < B;
  }
};
set<int, cmp> st;
set<int, cmp>::iterator it;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d%d", &len[i], &a[i], &x[i], &y[i], &mod[i]);
    sumlen += len[i];
  }
  int Max = 0;
  for (int i = 1; i <= n; i++) {
    int pre = a[i], now;
    ans = 0;
    for (int j = 2; j <= len[i]; j++) {
      now = (1ll * pre * x[i] % mod[i] + y[i]) % mod[i];
      if (now < pre) ans++;
      pre = now;
    }
    Max = max(Max, ans);
  }
  printf("%d\n", Max);
  if (sumlen <= 200000) {
    c[0] = 0;
    for (int i = 1; i <= n; i++) {
      c[i] = a[i];
      st.insert(i);
    }
    for (; sumlen--;) {
      it = st.lower_bound(0);
      if (it == st.end()) {
        int u = *st.begin();
        st.erase(st.begin());
        pt[++cnt][0] = c[u], pt[cnt][1] = u;
        len[u]--;
        c[0] = c[u];
        if (len[u]) {
          c[u] = (1ll * c[u] * x[u] % mod[u] + y[u]) % mod[u];
          st.insert(u);
        }
      } else {
        int u = *it;
        st.erase(it);
        pt[++cnt][0] = c[u], pt[cnt][1] = u;
        len[u]--;
        c[0] = c[u];
        if (len[u]) {
          c[u] = (1ll * c[u] * x[u] % mod[u] + y[u]) % mod[u];
          st.insert(u);
        }
      }
    }
    for (int i = 1; i <= cnt; i++) printf("%d %d\n", pt[i][0], pt[i][1]);
  }
}
