#include <bits/stdc++.h>
using namespace std;
int n, k, sumk, cnt;
long long a, x, y, m;
vector<vector<long long> > vec;
vector<pair<int, int> > ans;
int idx[1 << 13];
set<pair<int, int> > st;
set<pair<int, int> >::iterator it;
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;
  }
}
void solve() {
  int last = -1;
  int br = 0;
  for (int i = 0; i < n; i++) st.insert(make_pair((int)vec[i][0], i));
  while (1) {
    if (br == sumk) break;
    br++;
    int row;
    it = st.lower_bound(make_pair(last, -1));
    if (it == st.end()) {
      cnt++;
      it = st.begin();
    }
    row = (*it).second;
    st.erase(it);
    ans.push_back(make_pair(vec[row][idx[row]], row + 1));
    last = vec[row][idx[row]];
    idx[row]++;
    if (idx[row] < vec[row].size())
      st.insert(make_pair(vec[row][idx[row]], 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;
}
