#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, int> ans[200005];
int main() {
  int n, mx = 0, cur = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int k, c, x, y, m, cnt = 0;
    scanf("%d%d%d%d%d", &k, &c, &x, &y, &m);
    while (k--) {
      if (cur <= 200000) ans[cur++] = make_pair(make_pair(cnt, c), i);
      long long b = ((long long)c * x + y) % m;
      if (b < c && k) cnt++;
      mx = max(mx, cnt);
      c = b;
    }
  }
  printf("%d\n", mx);
  if (cur <= 200000) {
    sort(ans, ans + cur);
    for (int i = 0; i < cur; i++)
      printf("%d %d\n", ans[i].first.second, ans[i].second + 1);
  }
  return 0;
}
