A mischievous child was walking through the park when suddenly a bear appeared in his path. Fortunately, a very old witch appeared and saved the child from the bear's claws. However, the witch's help is not unconditional. To repay her, the witch brought her dwarfs and told the child:
"Look closely at the dwarfs. Each of them has three numbers written on their forehead. Let's denote the numbers on dwarf with , , and . We consider the number , which is equal to the number of gadfadarian numbers from to . You might wonder what a gadfadarian number is. Well, it is a number that has ones in its representation in base . I want to know . However, I will be generous today and only ask for the result modulo ."
Help the witch find the answer to the question!
Input data
The first line contains a natural number , representing the number of dwarfs.
The next lines contain the numbers on the dwarfs' foreheads, with representing the number written on the forehead of dwarf , representing the base referred to by the witch, and representing the number of ones in the base representation that we need. The number is given in base .
Output data
For each dwarf, output the required answer on a separate line, representing the value of modulo .
Constraints and clarifications
- has at most digits in base
# | Points | Constraints |
---|---|---|
1 | 11 | |
2 | 10 | |
3 | 15 | has at most digits, and for all questions |
4 | 11 | has at most digits, and for all questions |
5 | 23 | |
6 | 30 | No additional constraints |
Example 1
stdin
6
99 10 1
1110100011 2 3
1110100011 2 2
12103 5 2
681 9 0
999 10 2
stdout
18
120
45
219
377
27
Explanation
For the first example, valid numbers are , numbers from to except , and numbers , , , .