On the planet Divido teachers have a serious problem. Due to the spread of calculators the students cannot do long division with pencil and paper any longer. But now the planetarian government has announced a test for all students in basic schools to test their math skills. Because it has to be a multiple choice test the students only have to calculate the length of the period when dividing two numbers. Your task is to write a program which calculates the right solutions in advance to ease the correction of the test. An additional problem occurs because not all intelligent species on Divido have the same number of fingers and thus their systems of numbers have different bases.
You are given a base b, a numerator x, and a denominator y, the latter of which are integers to the base b.Then calculate the length of the period in the base b expansion of the fraction q = x/y. Remember that the digits of the base b expansion of the the rational number q are the coefficients of the powers of the base:
q = qnqn-1...q0.q-1q-2...=Σ-∞<=i<=nqibi
Here are some examples:
In the first two examples, there is a finite base b expansion of the fraction, meaning that all digits that follow are zero. In that case, we say that the length of the period is zero. Note that one could as well express the first example as 0.4(9)10 with a period length of one, but that is not what we want.
In the third example, however, the period length is unambiguously two.
The first line contains the number of fractions. For each fraction you are first given the base b as a decimal number with 2 <= b <= 36, and then the numerator x and the denominator y of the fraction to that base separated by single blanks. It is guaranteed that the denominator satisfies 0 < y <= 100 00010. Digits not in the range of 0 . . . 9 are represented by letters starting from "a" or "A" in the usual way, where case does not matter.
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the length of the period in the base b expansion of x/y , but make sure to print the length as a decimal number. Terminate the output for the scenario with a blank line.
10 1 2
3 1 10
10 2 11
36 HI ho
TUD Contest 2004,