Under construction

Still being built. Things may change

Bacteria Growth (Small)

Time limit5sMemory limit512 MB

Summary
Apply the map x to x^x exactly B times starting from A and report the result modulo C.
Level

Medium7 of 10

Topics
Number theory, Recursion
Solved
No attempts yet

Problem

Pascal studies microorganisms and found a bacterium with an unusual growth pattern. If xx of them are present at one moment, then in an ideal environment there are xxx^x of them one hour later.

Given three integers AA, BB and CC, compute how many bacteria there are BB hours after a colony of AA bacteria starts. Every hour the count xx turns into xxx^x, and that change happens BB times. The answer grows very large, so print its remainder modulo CC.

Input

The first line contains the number of test cases TT. Each of the following TT lines contains one test case.

A test case is three integers separated by spaces: AA, BB and CC, in that order.

Constraints

  • 1≤T≤5001 \le T \le 500
  • 1≤A≤10001 \le A \le 1000
  • 1≤B≤10001 \le B \le 1000
  • 1≤C≤10001 \le C \le 1000

Output

For each test case, print one line in the following format.

Case #X: N

XX is the number of the test case, counting from 1, and NN is the number of bacteria after BB hours, taken modulo CC.

Examples2

  1. Example 1

    Input
    5
    1 10 997
    2 2 997
    2 2 6
    461 1 709
    461 1000 709
    
    Expected output
    Case #1: 1
    Case #2: 256
    Case #3: 4
    Case #4: 674
    Case #5: 461
    
  2. Example 2

    Input
    3
    1 1 1
    2 1 100
    3 1 100
    
    Expected output
    Case #1: 0
    Case #2: 4
    Case #3: 27