#include <bits/stdc++.h>
using namespace std;
const int prime = 1003800021;
const int MOD = 1103800039;
int n, k, sumk, maxk, cnt;
long long a, x, y, m;
vector<vector<long long> > vec;
vector<vector<unsigned long long> > h;
unsigned long long pw[1 << 18];
vector<pair<int, int> > ans;
int idx[1 << 13];
inline unsigned long long getHash(int row, int le, int ri) {
  if (!le) return h[row][ri];
  return (h[row][ri] - h[row][le - 1] * pw[ri - le + 1] % MOD + MOD) % MOD;
}
struct cmp {
  bool operator()(pair<int, int> a, pair<int, int> b) {
    int i = a.first, j = b.first;
    int l1 = a.second, l2 = b.second;
    int lo = 1, hi = min(vec[i].size() - l1, vec[j].size() - l2), mid;
    int lcp = 0;
    while (lo <= hi) {
      mid = (lo + hi) >> 1;
      if (getHash(i, l1, l1 + mid - 1) == getHash(j, l2, l2 + mid - 1)) {
        lcp = mid;
        lo = mid + 1;
      } else
        hi = mid - 1;
    }
    if (l1 + lcp >= vec[i].size()) return false;
    if (l2 + lcp >= vec[j].size()) return true;
    int num1 = vec[i][l1 + lcp], num2 = vec[j][l2 + lcp];
    return num1 > num2;
  }
};
priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> pq;
void build(int idx) {
  vec[idx].resize(k);
  for (int i = 0; i < k; i++) {
    vec[idx][i] = a;
    a = (a * x + y) % m;
  }
}
void read() {
  scanf("%d", &n);
  vec.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> k >> a >> x >> y >> m;
    build(i);
    sumk += k;
    if (maxk < k) maxk = k;
  }
}
void solve() {
  int last = -1;
  int br = 0;
  while (1) {
    if (br == sumk) break;
    br++;
    int row = -1;
    for (int i = 0; i < n; i++)
      if (idx[i] < vec[i].size() && vec[i][idx[i]] >= last) {
        if (row == -1)
          row = i;
        else {
          if (vec[i][idx[i]] < vec[row][idx[row]]) row = i;
        }
      }
    if (row == -1) {
      cnt++;
      for (int i = 0; i < n; i++)
        if (idx[i] < vec[i].size()) {
          if (row == -1)
            row = i;
          else {
            if (vec[i][idx[i]] < vec[row][idx[row]]) row = i;
          }
        }
    }
    ans.push_back(make_pair(vec[row][idx[row]], row + 1));
    last = vec[row][idx[row]];
    idx[row]++;
  }
  printf("%d\n", cnt);
  if (sumk <= 200000) {
    for (int i = 0; i < ans.size(); i++)
      printf("%d %d\n", ans[i].first, ans[i].second);
  }
}
int main() {
  read();
  solve();
  return 0;
}
