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
2011-05-09 08:50:44 Aharon Smbatyan
t about 50000
2011-04-26 06:20:04 Lureohc Otnafifa
why I got WA..?
2010-12-19 16:46:47 dpcm
Is there any leading - zeros in b ??? No

Last edit: 2010-12-22 23:03:58
2010-11-14 01:21:23 Ahmad MustofaHadi
because she is not ordinary little sister...
2010-10-31 22:12:27 Piotr KÄ…kol
In PHP it also get TLE.
2010-10-31 05:04:42 .::Manish Kumar::.
ya ur right!
I too got TLE in python!

Last edit: 2010-10-31 04:26:23
2010-10-31 05:04:42 numerix
Edit numerix: Thanks for increasing timelimit to 2 s, but that is still not enough to get a Python solution AC. On the other hand solving this task in Python is very easy because of some builtin features, so timelimit shouldn't be increased further (or problem should be moved to tutorial section).

Last edit: 2010-10-31 13:55:04
2010-10-31 05:04:42 .::Manish Kumar::.
:)

Last edit: 2010-10-31 04:24:55
2010-10-31 05:04:42 numerix
Tooo much I/O for Python ... calculation is not the problem.


Last edit: 2010-10-31 05:10:51
2010-10-31 05:04:42 .::Manish Kumar::.
:P

Last edit: 2010-10-31 04:25:26
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.