Be Efficient

Consider an integer sequence consisting of N elements, where: X0 = A Xi = ((Xi−1 ∗ B + C)%M) + 1 for i = 1 to N − 1 You will be given the values of A, B, C, M and N. Find out the number of consecutive subsequences whose sum is a multiple of M. Consider an example where A = 2, B = 1, C = 2, M = 4 and N = 4. So,X0 =2,X1 =1,X2 =4andX3 =3. The consecutive subsequences are {2}, {2 1}, {2 1 4}, {2 1 4 3}, {1}, {1 4}, {1 4 3}, {4}, {4 3} and {3}. Of these 10 ‘consecutive subsequences’, only two of them adds up to a figure that is a multiple of 4 — {1 4 3} and {4}. Input The first line of input is an integer T (T < 500) that indicates the number of test cases. Eact case consists of 5 integers A, B, C, M and N. A, B and C will be non-negative integers not greater than 1000. N and M will be a positive integers not greater than 10000. Output For each case, output the case number followed by the result. Sample Input 2 21244 923 278 195 8685 793 Sample Output Case 1: 2 Case 2: 34