#include <bits/stdc++.h>
#pragma comment(linker, "/stack:256000000")
using namespace std;
const int N = 200100, M = 200500, INF = 1000 * 1000 * 1000 + 7;
const long double eps = 1e-9;
template <typename X>
X gcd(X a, X b) {
  while (b) b ^= a ^= b ^= a %= b;
  return a;
}
template <typename V>
V dist(V x1, V y1, V x2, V y2) {
  return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int n;
vector<int> a[2];
bool read() {
  if (scanf("%d", &n) != 1) {
    return 0;
  }
  a[0].clear();
  a[1].clear();
  for (int i = 0; i < n; ++i) {
    long long k, aa, x, y, m;
    scanf("%I64d%I64d%I64d%I64d%I64d", &k, &aa, &x, &y, &m);
    for (int j = 0; j < k; ++j) {
      a[i].push_back(aa);
      aa = (aa * x + y) % m;
    }
  }
  return 1;
}
bool solve() {
  if (!read()) return 0;
  vector<pair<int, int>> ans;
  int idx[2] = {0};
  int prev = -1;
  for (; idx[0] < a[0].size() && idx[1] < a[1].size();) {
    set<pair<int, int>> k;
    for (int i = 0; i < n; ++i)
      if (a[i][idx[i]] >= prev) k.insert(make_pair(a[i][idx[i]], i));
    if (k.size() != 0) {
      ans.push_back(*k.begin());
      idx[k.begin()->second]++;
      prev = k.begin()->first;
    } else {
      for (int i = 0; i < n; ++i) k.insert(make_pair(a[i][idx[i]], i));
      ans.push_back(*k.begin());
      idx[k.begin()->second]++;
      prev = k.begin()->first;
    }
  }
  for (int i = 0; i < n; ++i)
    for (; idx[i] < a[i].size(); idx[i]++) {
      ans.push_back(make_pair(a[i][idx[i]], i));
    }
  int cnt = 0;
  for (int i = 0; i < ans.size(); ++i) {
    if (i + 1 < ans.size() && ans[i].first > ans[i + 1].first) {
      cnt++;
    }
  }
  printf("%d\n", cnt);
  if (ans.size() <= 200000)
    for (int i = 0; i < ans.size(); ++i) {
      printf("%d %d\n", ans[i].first, ans[i].second + 1);
    }
  return 1;
}
int main() {
  while (solve())
    ;
  return 0;
}
