Alice is thrilled about participating in the “Take-off Programming Contest Spring 2023”, but she realizes that number theory is one of her weak areas. Determined to overcome this challenge, Alice dives into the study of number theory and learns about two crucial concepts: digit sum and divisors of a number. Empowered by her newfound knowledge, Alice devises a new problem to put her understanding to the test.
Given an integer , Alice wants to find the maximum digit sum among all the divisors of . However, Alice encounters difficulties and cannot solve the problem alone. Now, it's your task to help Alice find a solution.
Formally, a divisor of is any positive integer that evenly divides without leaving a remainder. Your goal is to find the divisor of that yields the highest sum of its digits. For example, consider the number . The divisors of are , and . The sum of digits for each divisor is as follows:
In this case, the maximum sum of digits among the divisors of is .
Write a program that can efficiently solve this problem for various test cases.
The first line of the input contains an integer — representing the number of test cases.
Each of the next lines contains a positive integer — representing a test case.
For each test case, output a single line with an integer — representing the maximum sum of digits among all the divisors of .
Input | Output |
---|---|
4 12 9 34 22 | 6 9 8 4 |
As the numbers involved can be large and have multiple test cases, ensure that your solution runs efficiently within the given constraints.
Be careful about the new line (‘\n’) at the end.