vrajitoare

Time limit: 0.6s Memory limit: 32MB Input: Output:

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 ii with viv_i, bib_i, and xix_i. We consider the number mim_i, which is equal to the number of gadfadarian numbers from 00 to viv_i. You might wonder what a gadfadarian number is. Well, it is a number that has xix_i ones in its representation in base bib_i. I want to know mim_i. However, I will be generous today and only ask for the result modulo 109+710^9 + 7."

Help the witch find the answer to the question!

Input data

The first line contains a natural number nn, representing the number of dwarfs.

The next nn lines contain the numbers on the dwarfs' foreheads, with viv_i representing the number written on the forehead of dwarf ii, bib_i representing the base referred to by the witch, and xix_i representing the number of ones in the base bib_i representation that we need. The number is given in base bib_i.

Output data

For each dwarf, output the required answer on a separate line, representing the value of mim_i modulo 109+710^9 + 7.

Constraints and clarifications

  • 1n1001 \leq n \leq 100
  • viv_i has at most 10510^5 digits in base bib_i
  • 2bi102 \leq b_i \leq 10
  • 0xivi0 \leq x_i \leq |v_i|
# Points Constraints
1 11 vi103v_i \leq 10^3
2 10 vi106v_i \leq 10^6
3 15 viv_i has at most 10310^3 digits, bi=2b_i = 2 and xi1x_i \leq 1 for all questions
4 11 viv_i has at most 10310^3 digits, bi=2b_i = 2 and xi2x_i \leq 2 for all questions
5 23 vi1018v_i \leq 10^{18}
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 11, numbers from 1010 to 1919 except 1111, and numbers 2121, 3131, \dots, 9191.

Log in or sign up to be able to send submissions!