Fredo and Maths
Tag(s):

## Euler's totient function, Mathematics, Medium, Modular exponentiation, Prime Factorization, Sieve

Problem
Editorial
Analytics

Given three numbers x,k and m, you need to find the value of $x^{x^{x^{.^{.^{.^x}}}}}$%m where number of x's in the expression are k. That is, if x=5,k=3 and m=3, then you need to compute $5^{5^5}$%3.

Constraints:
$1 \le T \le 10^5$
$1 \le m \le 10^7$
$1 \le k \le 10^{18}$
$m < x \le 10^8$ x is always a prime number

Format of the input file:
First line : T i.e number of testcases.
For each testcase :
First line : Three space separated integers x , k and m.

Format of the output file:
Print the answer for each test case in a separate line

SAMPLE INPUT
2
5 1 3
5 2 3

SAMPLE OUTPUT
2
2

Explanation

Case 1: 5 % 3=2
Case 2: $5^5$ % 3=3125 % 3= 2

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, TypeScript, Visual Basic

## CODE EDITOR

Initializing Code Editor...

## This Problem was Asked in Challenge Name

April Circuits '17

OTHER PROBLEMS OF THIS CHALLENGE
• Basic Programming > Implementation
• Algorithms > Searching
• Algorithms > Dynamic Programming
• Math > Number Theory
• Data Structures > Advanced Data Structures
• Data Structures > Advanced Data Structures