Leftmost Divisor
Submissions: 689   Accuracy:

10.41%

  Difficulty: Medium   Marks: 4

For any two given positive integers P and Q, find (if present) the leftmost digit in the number obtained by computing the exponent PQ i.e. P raised to the power Q, such that it is a divisor of PQ.

Input:

The first line of input is an integer T, denoting the number of test cases. For each test case, input two integers P and Q , denoting the base and the power respectively.

Output:

For each test case, there is only one line of output denoting the left-most digit in the number PQ which is a divisor of PQ. If no such digit exists, print -1. Each output is printed on a new line.

Note: PQ may be a very huge number, so if the number of digits in PQ is greater than 18, you need to extract the first 10 digits of the result and  check if any digit of that number divides the number.

Constraints:

1<=T<=100

1<=P,Q<=50
Example:

Input:

5
1 1
5 2
3 3
1 2
5 3

Output:

1
5
-1
1
1

Explanation:

In the first test case, 1 raised to the power 1 gives 1, which is a divisor of itself i.e. 1.

The exponent 5 raised to the power 2 gives 25, wherein leftmost digit 2, does not divide 25 but 5 does. Hence output is 5.

In the third, 3 raised to the power 3, 27, has no such digit which divides itself. Hence, output is -1.

In the fourth test case, 1 raised to the power 2 gives 1, where 1 divides 1. Hence, output is 1.

In the fifth test case, 5 raised to the power 3 is 125. Both 1 and 5 is a divisor. leftmost is 1. Hence, 1 is the output.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Anushree_20


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.