#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 6010;
int n;
vector<int> a[MAXN];
vector<vector<pair<int, int> > > s[MAXN];
int split(vector<int> &a, int id) {
  reverse((a).begin(), (a).end());
  int level = 0;
  while (!a.empty()) {
    vector<pair<int, int> > ans(1, make_pair(a.back(), id));
    a.pop_back();
    while (!a.empty() && a.back() >= ans.back().first) {
      ans.push_back(make_pair(a.back(), id));
      a.pop_back();
    }
    reverse((ans).begin(), (ans).end());
    s[level].push_back(ans);
    level++;
  }
  return level;
}
void merge(vector<pair<int, int> > &ans, vector<vector<pair<int, int> > > &s) {
  set<pair<pair<int, int>, int> > st;
  for (int i = 0; i < (int)(s.size()); i++)
    st.insert(make_pair(s[i].back(), i));
  while (!st.empty()) {
    int id = st.begin()->second;
    st.erase(st.begin());
    ans.push_back(s[id].back());
    s[id].pop_back();
    if (!s[id].empty()) st.insert(make_pair(s[id].back(), id));
  }
}
int main() {
  cin >> n;
  int total = 0;
  for (int i = 0; i < (int)(n); i++) {
    int k;
    long long a1, x, y, m;
    scanf("%d%I64d%I64d%I64d%I64d", &k, &a1, &x, &y, &m);
    for (int j = 0; j < (int)(k); j++) {
      a[i].push_back((int)a1);
      a1 = (a1 * x + y) % m;
    }
    total += (int)a[i].size();
  }
  int res = 0;
  for (int i = 0; i < (int)(n); i++) {
    int cur = 0;
    for (int j = 0; j < (int)(a[i].size() - 1); j++)
      if (a[i][j] > a[i][j + 1]) cur++;
    res = max(res, cur);
  }
  vector<pair<int, int> > ans;
  if (total <= 200000) {
    int maxlevel = 0;
    for (int i = 0; i < (int)(n); i++) maxlevel = max(maxlevel, split(a[i], i));
    for (int i = 0; i < (int)(maxlevel); i++) merge(ans, s[i]);
  }
  cout << res << endl;
  if (total <= 200000)
    for (int i = 0; i < (int)(ans.size()); i++)
      printf("%d %d\n", ans[i].first, ans[i].second + 1);
  return 0;
}
