#include <bits/stdc++.h>
using namespace std;
int k[5010], a[5010], x[5010], y[5010], m[5010], n;
struct compare {
  bool operator()(int i, int j) { return a[i] <= a[j]; }
};
vector<pair<int, int> > ans;
multiset<int, compare> q;
int main() {
  scanf("%d", &n);
  if (n == 2) {
    int cnt = 0, bp = 0, last = -1, lv = -2147483647;
    for (int i = 0; i < (n); i++) {
      scanf("%d%d%d%d%d", &k[i], &a[i], &x[i], &y[i], &m[i]);
      cnt += k[i]--;
      q.insert(i);
    }
    while (!q.empty()) {
      multiset<int, compare>::iterator i = q.end();
      if (last == -1) i = q.upper_bound(n);
      if (i == q.end()) i = q.begin();
      int cur = *i;
      q.erase(i);
      if (a[cur] < lv) bp++;
      a[n] = a[cur] - 1;
      lv = a[cur];
      if (cnt <= 200000) ans.push_back(pair<int, int>(cur, a[cur]));
      if (k[cur]) {
        k[cur]--;
        a[cur] = (a[cur] * 1ll * x[cur] + y[cur]) % m[cur];
        q.insert(cur);
      }
    }
    printf("%d\n", bp);
    for (int i = 0; i < (ans.size()); i++)
      printf("%d %d\n", ans[i].second, ans[i].first + 1);
  }
}
