We define a recursive function f(n) which takes a positive integer n as input and does the following:

*f(n):*

*if n < 10 *

* return n*

*else return f( sum_of_digits(n) )*

where sum_of_digits(n) returns the sum of the digits of the positive integer n.

Given two positive integers **A** and **B, **the task is to find the value of **f**(**A ^{B}**).

**Input: **

The first line of input contains a single integer **T** denoting the number of test cases. Then **T** test cases follow. Each test case consists of exactly one line. This line consists of two space separated positive integers **A** and **B**.

**Output:**

Corresponding to each test case, in a new line, print the value of f(**A ^{B}**).

**Constraints:**

1 ≤ **T** ≤ 200

1 ≤ **A**, **B** ≤ 10^{9 }

**Example:**

**Input**

3

6 6

7 3

1000 1

**Output**

9

1

1

**Explanation**:

f(6^{6}) = f(46656) = f(27) = f(9) = 9

f(7^{3}) = f(343) = f(10) = f(1) = 1

f(1000) = f(1) = 1

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.

codersanjeev | 66 |

snow_den_ | 60 |

BoggavarapuRamSaranSaiSrinivasGupta | 60 |

rajupraaa1234 | 53 |

SUZAKU | 49 |

mr_kksparrow | 433 |

manvirag982 | 258 |

snow_den_ | 232 |

arpit_anshuman | 228 |

SoumyaKaushik | 205 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.