#include <bits/stdc++.h>
using namespace std;
vector<int> E[5000 + 5];
vector<pair<int, int> > ans;
set<pair<int, pair<int, int> > > ss;
set<pair<int, pair<int, int> > >::iterator it;
int n, nprob, ret, tot = 0, sum = 0;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int k, val, mod, x, y, tmp = 0;
    scanf("%d %d %d %d %d", &k, &val, &x, &y, &mod);
    E[i].push_back(val);
    nprob++;
    ss.insert(pair<int, pair<int, int> >(val, pair<int, int>(i, nprob)));
    sum += k;
    for (int j = 2; j <= k; j++) {
      val = (1ll * val * x + y) % mod;
      if (E[i].size() > 0 && val < E[i].back()) tmp++;
      E[i].push_back(val);
    }
    tot = max(tot, tmp);
    reverse(E[i].begin(), E[i].end());
  }
  if (sum > 200000) {
    printf("%d", tot);
    return 0;
  }
  while (!ss.empty()) {
    int last_val = (ans.empty() ? 0 : ans.back().first);
    it = ss.lower_bound(
        pair<int, pair<int, int> >(last_val, pair<int, int>(0, 0)));
    if (it != ss.end()) {
      ans.push_back(pair<int, int>((*it).first, (*it).second.first));
      int id = (*it).second.first;
      ss.erase((*it));
      E[id].pop_back();
      if (E[id].size() > 0) {
        nprob++;
        ss.insert(pair<int, pair<int, int> >(E[id].back(),
                                             pair<int, int>(id, nprob)));
      }
    } else {
      ret++;
      it = ss.begin();
      ans.push_back(pair<int, int>((*it).first, (*it).second.first));
      int id = (*it).second.first;
      ss.erase((*it));
      E[id].pop_back();
      if (E[id].size() > 0) {
        nprob++;
        ss.insert(pair<int, pair<int, int> >(E[id].back(),
                                             pair<int, int>(id, nprob)));
      }
    }
  }
  printf("%d\n", ret);
  for (int i = 0; i < ans.size(); i++)
    printf("%d %d\n", ans[i].first, ans[i].second);
}
