Be Efficient#1570

Be Efficient

1 second
128 megabytes
Very easy

Consider an integer sequence consisting of elements:

x_0 = a,\\ x_i = ((x_{i-1} \cdot b + c) \bmod m) + 1, \quad i = 1, 2, \ldots, n-1

You are given , , , , and . Find the number of consecutive subsequences whose sum is divisible by .

For example, let a = 2, b = 1, c = 2, m = 4, n = 4. Then

x_0 = 2,\\ x_i = (x_{i-1} + 2) \bmod 4 + 1, \quad i = 1, 2, 3, 4

So, x_0 = 2, x_1 = 1, x_2 = 4, x_3 = 3. The consecutive subsequences are , , \{2, 1, 4\}, \{2, 1, 4, 3\}, , , \{1, 4, 3\}, , , and . Of these consecutive subsequences, only two have a sum divisible by : \{1, 4, 3\} and .

Input

The first line contains the number of tests (t < 500). Each of the following lines contains a separate test, consisting of five integers: , , , , (0 \leq a, b, c \leq 1000, 0 < m, n \leq 10000).

Output

For each test, output its number and the required result on a separate line.

Examples

Example 1

Loading
Loading