1323 : A Simple Problem

时间限制:1 Sec 内存限制:256 MiB 提交:3 正确:3

提交 状态 论坛

题目描述

Haze is weak in English, so he would like to description this problem as simple as possible. He want you to calculate a formula that is F(a,b,k,p).

$ F(a,b,k,p)=a^{b^{b^{b\cdots (k\, times)}}}\% p $

输入描述

The first line consist a number T presented the number of test case. Each of following T lines has four figures, a,b,k,p.

Limits:

$1 \leq T \leq 1000, 1 \leq  a,b,p \leq 10^7, 1 \leq k \leq 10^{18}$

输出描述

Print T lines, and every line has the result for the four parameters. 

样例输入

3
2 3 1 10000
3 2 2 80
342 234 567 89

样例输出

8
1
2

来源

第二届湖北省赛