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

**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 P^{Q} which is a divisor of P^{Q}. If no such digit exists, print -1. Each output is printed on a new line.

**Note: **P^{Q }may be a very huge number, so if the number of digits in P^{Q} 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.

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.

bhatabhi554 | 317 |

hardikJain5 | 306 |

CodeBuddy | 293 |

Akkki111 | 233 |

clone | 230 |

KshatriyaYash | 1932 |

nikhil_sojan | 1374 |

lonecoder | 1236 |

mazumderrohit8 | 1176 |

RemoveTLE | 1144 |

blackshadows | 5327 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4605 |

Quandray | 4444 |

Login to report an issue on this page.