#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
struct Node {
  int id, cost, cnt;
  Node(int _i, int _cost, int _cnt) {
    id = _i;
    cost = _cost;
    cnt = _cnt;
  }
  bool operator<(const Node &n) const {
    if (cnt != n.cnt)
      return cnt < n.cnt;
    else
      return cost < n.cost;
  }
};
vector<Node> ans;
int main() {
  int ret = 0, n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int k, v, x, y, m, pre = -1, idx = 1;
    scanf("%d %d %d %d %d", &k, &v, &x, &y, &m);
    for (int j = 0; j < k; j++) {
      if (v < pre) idx++;
      ans.push_back(Node(i + 1, v, idx));
      pre = v;
      v = (v * 1LL * x + y) % m;
    }
    ret = max(ret, idx - 1);
  }
  printf("%d\n", ret);
  if (ans.size() <= 200000) {
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      printf("%d %d\n", ans[i].cost, ans[i].id);
    }
  }
  return 0;
}
