Timer is Running

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.

visutkarsh2000 | 251 |

ayushaggarwal821 | 248 |

ashishboss9977 | 236 |

tekuripurushotham | 178 |

ramharshrai1999 | 151 |

abnandi04 | 776 |

KshamaGupta | 499 |

ponnadasaikesav | 450 |

ayushaggarwal821 | 415 |

yogesh_aka_dante | 405 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.