Problem hidden
This problem was hidden by Editorial Board member probably because it has incorrect language version or invalid test data, or description of the problem is not clear.

C24E08BT - EC08B Random Numbers (T)

Linear congruential generators (LCGs) are simple random number generators of the form
Xi+1 = (aXi + c) mod m

LCGs are periodic, because due to "mod m" there will be a smallest P > 0 (the length of the period) where Xi+P = Xi for some i. You have to look at the first P elements of the output from the LCG starting with X0 = 0. The output of an LCG is the number it provides for our use which we calculate as
Yi = Xi mod k
where k is yet another constant.

You have to look for repetitions in this list of Y0 ... YP-1. A repetition is a sequence of numbers which occurs in two different positions in this list. For every LCG given in the input you have to output the length of the longest repetition. (Yi ... Yi+R-1 is a repetition of length R if there is a j <> i for which Yi+x = Yj+x for every x for which 0 <= i + x < P, 0 <= j + x < P and 0 < x < R.)

Input

The first line on the input contains the number of LCGs described in the file. The rest of the file contains one line for every LCG that contains the four numbers a, c, m and k separated by spaces.

Output

Every line of the output file must contain a single number that is the length of the longest repetition. If there are no repetitions (all numbers in Y are different), output 0.

This is the text variant of the problem. Read more here about submitting solutions!

Example

Input
1 1 4 2
Output
2

In this case X = {0, 1, 2, 3}, Y = {0, 1, 0, 1} and the longest repetition is {0, 1}.


Added by:Jacek DÄ…browski
Date:2009-02-04
Time limit:0.200s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:C++ 4.3.2 TEXT
Resource:Challenge 24 2008 Electronic Contest

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.