De-RNG-ed (Small)

Print the forced next term of K consecutive outputs from a linear generator modulo an unknown prime up to 10^D, or report that it is unknown.

Medium6Brute forceNumber theoryMathNo attempts yetTime limit5sMemory limit512 MB

Problem

I want to build an online poker site. One important part of such a system is the random number generator. It has to be fast and random enough. Here is the compromise I came up with.

I need to generate random numbers of length at most DD. First I pick a prime PP with P10DP \le 10^D. Then I pick non-negative integers AA and BB. Finally I pick an integer seed SS with 0SP10 \le S \le P-1.

To print my sequence of pseudo-random numbers I first print SS, then update SS with

S:=(A×S+B)modPS := (A \times S + B) \bmod P

I print the updated SS as the next term of the sequence and update it again with the same formula. I repeat that as often as I want.

Do you think this is a good random number generator? Write a program that reads KK consecutive terms of a sequence produced this way and prints the term that comes next.

Input

The first line contains the number of test cases TT. The first line of each test case contains DD and KK. The next line contains KK consecutive terms produced by a random number generator of the kind described above.

Limits

  • 1T1001 \le T \le 100
  • 1D41 \le D \le 4
  • 1K101 \le K \le 10
  • The KK given integers are consecutive terms of a sequence produced by a generator of the kind described above.

Output

For each test case print one line in the form Case #x: y, where xx is the test case number starting from 1.

Consider every choice of a prime P10DP \le 10^D and non-negative integers AA and BB that produces the KK given terms. If all of those choices give the same next term, then yy is that value. If two or more different next terms are possible, then yy is I don't know.