CSQUARE - Powered and Squared

Description

Marko is learning method of successive squaring so that he can calculate a^b mod m quickly. To give himself practice he wrote many tuples of a, b and m and went to school thinking that he will do it after school.

After school he found that tuples he wrote are modified by his little sister. His sister converted each b into base 3. Marko wrote everything in base 10.

Help Marko to do his excercise.

Input

First line of input contains a number T, number of test cases. Then T test cases follows each containing three numbers a (<=10^9), b and m (<=10^5) (a in base 10, b in base 3 and m in base 10). Number of digits in b will be less than 250.

Output

Output a number for each test case a^b mod m in base 10.

Sample

Input:
2
2 10 10
3 21101 19

Output:
8
3

Added by:Divyanshu Ranjan
Date:2010-10-26
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM32-GCC GAWK MAWK BC C-CLANG NCSHARP CPP14-CLANG CLOJURE COBOL COFFEE D-CLANG D-DMD DART ELIXIR FSHARP FANTOM FORTH GO GOSU GRV JS-MONKEY JULIA KTLN NIM NODEJS OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 PYTHON3 PY_NBC R RACKET RUST CHICKEN SED SWIFT UNLAMBDA VB.NET

hide comments
2021-06-01 14:07:06
Hint: Don't convert the number into decimal or binary. Use the ternary number itself.
2021-01-13 14:19:42
I don't have a little sister but molesting stray dogs is also a great exercise as far as I'm concerned.

Last edit: 2021-01-13 22:34:25
2020-10-03 08:36:50
which type of sister does that
2020-05-23 21:54:49
Please also consider java submissions! Atleast keep the time limit different for all languages according to their speeds
2019-01-30 21:43:28
To the problem setters: please think about java solutions
2018-11-23 18:16:18
don't know why tile limit is strict for java.....submitted successfully with the same logic in c++...help me how can I reduce TLE in java any resources to study?
2016-04-15 17:41:44 minhthai
time limit too strict for java :(
2015-11-03 19:12:11 Himanshu Yadav
@master_mind you must be converting the the base-3 number into base-10 without using modulo.
2015-05-28 22:02:30 Vaporeon
"'>>'" does the trick! ;) :D
2015-04-01 14:57:03 Archit Jain
cin and cout giving tle
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.