De-RNG-ed (Large)

Given K consecutive outputs of a linear congruential generator with unknown prime modulus up to 10^D, decide the uniquely forced next term or report ambiguity.

Medium7Number theoryBrute forceMathNo attempts yetTime limit5sMemory limit512 MB

Problem

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

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

The sequence comes out like this. First I output SS, then I update SS with

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

I output the updated SS as the next term of the sequence and update SS again with the same formula. I repeat this as many times as I want.

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

Input

The first line has the number of test cases TT. Each test case then takes two lines. The first line has DD and KK. The second line has KK consecutive terms of a sequence made by the generator described above.

Limits

  • 1T1001 \le T \le 100
  • 1D61 \le D \le 6
  • 1K101 \le K \le 10
  • The KK integers are consecutive terms of a sequence made by a generator of the kind above, so at least one (P,A,B)(P, A, B) produces exactly these KK terms.

Output

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

Consider every combination of a prime P10DP \le 10^D and non-negative integers AA and BB that produces exactly the given KK terms. If all of those combinations give the same next term, print that value as yy. If two of them give different next terms, print I don't know. as yy, period included.