参考资料:

https://blog.nowcoder.net/n/36a6859228044debada0e4c88109afb8

https://blog.nowcoder.net/n/4711e1f61ced4eac9866a936c876d2ca

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static long power(long base,long exp,long mod){
        long res=1%mod;
        base%=mod;
        while(exp>0){
            if(exp%2==1){
                res=(res*base)%mod;
            }
            base=(base*base)%mod;
            exp/=2;
        }
        return res;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T=in.nextInt();
        while(T-->0){
            long a=in.nextLong();
            long b=in.nextLong();
            long p=in.nextLong();
            System.out.println(power(a,b,p));
        }
    }
}