For any function that maps a finite set to itself, and for any initial value in the set, the sequence of values eventually repeats some values, i.e., there is some and some such that . Once this happens, the sequence continues by repeating the cycle from to .
For instance, the function that maps to generates the following sequence when :
In this sequence, the beginning of the cycle is found after 2 steps. In this case, , , and the periodicity is .
Given a function that maps the interval to itself, and several starting values , compute the corresponding values of and .
Input starts with the number of cases. Every such case begins with two integer numbers and . Follow, in order, the images of the numbers in . Follow numbers: the ’s for which the result must be computed.
For every case, print its number and lines each one with and .
Since some of the private cases are huge, a recursive program may exhaust the recursion stack.
Input
3 9 1 6 6 0 1 4 3 3 4 0 2 3 3 2 1 0 0 1 2 4 3 1 2 3 2 1 0 1
Output
Case #1: 3 2 Case #2: 2 0 1 0 2 0 Case #3: 2 1 2 2 2 1