#include<bits/stdc++.h> #define int long long usingnamespace std;
constint N = 2e5 + 66;
inlineintread() { ints(0), w(1); char ch = getchar(); while (ch < '0' || ch > '9') {if (ch == '-') w = -1; ch = getchar();} while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; }
inlinevoidput(int x) { if (! x) putchar('0'); if (x < 0) putchar('-'), x = -x; intnum(0); char c[66]; while (x) c[++ num] = x % 10 + 48, x /= 10; while (num) putchar(c[num --]); return (void)(putchar('\n')); }
int k, n; int a[N], res;
inlineintcalc() { int i, j, ans(0); for (i = 1; i <= n; i = j + 1) { j = i; ++ ans; while (a[j] < a[j + 1]) ++ j; } return ans - 1; }
signedmain() { int i, t; k = read(); for (t = 1; t <= k; ++ t) { n = read(), a[1] = read(); int x = read(), y = read(), p = read(); for (i = 2; i <= n; ++ i) a[i] = (a[i - 1] * x + y) % p; res = max(res, calc()); } put(res); return0; }