#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 4;
ll MOD;
#define mod(x) ((x)%MOD)
struct mat{
ll m[maxn][maxn];
}unit;
mat operator *(mat a,mat b){
mat ret;
ll x;
for(ll i = 0;i < maxn;i++){
for(ll j = 0;j < maxn;j++){
x = 0;
for(ll k = 0;k < maxn;k++)
x += mod((ll)a.m[i][k]*b.m[k][j]);
ret.m[i][j] = mod(x);
}
}
return ret;
}
void init_unit(){
for(ll i = 0;i < maxn;i++)
unit.m[i][i] = 1;
return ;
}
mat pow_mat(mat a,ll n){
mat ret = unit;
while(n){
if(n&1) ret = ret*a;
a = a*a;
n >>= 1;
}
return ret;
}
ll quick_pow(ll a,ll n){
ll ans = 1;
while(n){
if(n&1) ans = (ans*a)%MOD;
a = (a*a)%MOD;
n >>= 1;
}
return ans;
}
int main(){
ll n,A,B,C,P,T,ans;
init_unit();
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld%lld%lld",&n,&A,&B,&C,&P);
MOD = P;
if(n == 1ll) puts("1");
else if(n == 2ll) printf("%lld\n",quick_pow(A,B));
else{
if(A%P == 0) {printf("0\n");continue;}
mat a,b;
b.m[0][0] = C,b.m[0][1] = 1,b.m[0][2] = 0,b.m[0][3] = 0;
b.m[1][0] = 1,b.m[1][1] = 0,b.m[1][2] = 1,b.m[1][3] = 0;
b.m[2][0] = 0,b.m[2][1] = 0,b.m[2][2] = 0,b.m[2][3] = 0;
b.m[3][0] = 1,b.m[3][1] = 0,b.m[3][2] = 0,b.m[3][3] = 1;
a.m[0][0] = B,a.m[0][1] = 0,a.m[0][2] = 0,a.m[0][3] = B;
MOD--;
b = pow_mat(b,n-2);
a = a*b;
MOD++;
printf("%lld\n",quick_pow(A,a.m[0][0]));
}
}
return 0;
}