#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = 1000 * 1000 * 1000;
const long long LINF = 1000000000000000000LL;
const double eps = 1e-9;
void prepare(string s) {}
const int MAXN = 10 * 1000 + 41;
const int CNTANS = 200 * 1000;
int nn[MAXN];
long long a00[MAXN];
long long xx[MAXN];
long long yy[MAXN];
long long MOD[MAXN];
bool solve() {
  int N;
  cin >> N;
  int sum = 0;
  set<pair<long long, int> > s;
  for (int i = 0; i < N; ++i) {
    cin >> nn[i] >> a00[i] >> xx[i] >> yy[i] >> MOD[i];
    s.insert(pair<long long, int>(a00[i], i));
    sum += nn[i];
  }
  vector<pair<int, int> > ans;
  int prec = -1;
  int res = 0;
  while (!s.empty()) {
    set<pair<long long, int> >::iterator it =
        s.lower_bound(pair<long long, int>(prec, -1));
    if (it == s.end()) {
      it = s.begin();
      ++res;
    }
    if (sum <= CNTANS) ans.push_back(pair<int, int>(it->first, it->second));
    prec = it->first;
    int i = it->second;
    s.erase(it);
    if (--nn[i]) {
      a00[i] = (a00[i] * xx[i] + yy[i]) % MOD[i];
      s.insert(pair<long long, int>(a00[i], i));
    }
  }
  cout << res << endl;
  for (int i = 0; i < (int)ans.size(); ++i)
    cout << ans[i].first << ' ' << ans[i].second + 1 << endl;
  return false;
}
int main() {
  prepare("");
  while (solve()) {
  }
  return 0;
}
