//FIBOSQRT
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned int
long long MOD;
void multiply(ll a[3][3],ll b[3][3])
{
long long p,q,r,s,t,u,v,w,x;
p=(long long)a[0][0]*b[0][0]+(long long)a[0][1]*b[1][0]+(long long)a[0][2]*b[2][0];
q=(long long)a[1][0]*b[0][0]+(long long)a[1][1]*b[1][0]+(long long)a[1][2]*b[2][0];
r=(long long)a[2][0]*b[0][0]+(long long)a[2][1]*b[1][0]+(long long)a[2][2]*b[2][0];
s=(long long)a[0][0]*b[0][1]+(long long)a[0][1]*b[1][1]+(long long)a[0][2]*b[2][1];
t=(long long)a[1][0]*b[0][1]+(long long)a[1][1]*b[1][1]+(long long)a[1][2]*b[2][1];
u=(long long)a[2][0]*b[0][1]+(long long)a[2][1]*b[1][1]+(long long)a[2][2]*b[2][1];
v=(long long)a[0][0]*b[0][2]+(long long)a[0][1]*b[1][2]+(long long)a[0][2]*b[2][2];
w=(long long)a[1][0]*b[0][2]+(long long)a[1][1]*b[1][2]+(long long)a[1][2]*b[2][2];
x=(long long)a[2][0]*b[0][2]+(long long)a[2][1]*b[1][2]+(long long)a[2][2]*b[2][2];
//matrix res(3,vector<ll>(3));
a[0][0]=(ll)(p%MOD);
a[1][0]=(ll)(q%MOD);
a[2][0]=(ll)(r%MOD);
a[0][1]=(ll)(s%MOD);
a[1][1]=(ll)(t%MOD);
a[2][1]=(ll)(u%MOD);
a[0][2]=(ll)(v%MOD);
a[1][2]=(ll)(w%MOD);
a[2][2]=(ll)(x%MOD);
//return res;
}
void power(ll a[3][3],ll b[3][3],long long n)
{
if(n<=1)
return ;
else if(n&1)
{
power(a,b,n-1);
multiply(a,b);
}
else {
power(a,b,n>>1);
multiply(a,a);
}
}
int main()
{
int t;
long long n;
ll a,b;
scanf("%d",&t);
while(t--)
{
scanf("%u%u%u%lld",&a,&b,&MOD,&n);
ll f[3],temp[3][3];
f[0]=(ll)sqrt(3+(long long)a*b);//G(0)
f[1]=b;//f(1)
f[2]=a;//f(0)
ll t[3][3];
t[0][0]=temp[0][0]=1;
t[0][1]=temp[0][1]=1;
t[0][2]=temp[0][2]=0;
t[1][0]=temp[1][0]=2;
t[1][1]=temp[1][1]=1;
t[1][2]=temp[1][2]=1;
t[2][0]=temp[2][0]=0;
t[2][1]=temp[2][1]=1;
t[2][2]=temp[2][2]=0;
//cout<<"Executing..."<<endl;
power(t,temp,n-1);
//cout<<"Executing..."<<endl;
ll ans=(ll)(((long long)t[1][0]*f[0]+(long long)t[1][1]*f[1]+(long long)t[1][2]*f[2])%MOD);
printf("%u\n",(ll)ans);
}
return 0;
}
Ly9GSUJPU1FSVAoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsICAgICB1bnNpZ25lZCBpbnQKbG9uZyBsb25nIE1PRDsKCnZvaWQgbXVsdGlwbHkobGwgYVszXVszXSxsbCBiWzNdWzNdKQp7CgogICAgbG9uZyBsb25nIHAscSxyLHMsdCx1LHYsdyx4OwoKICAgIHA9KGxvbmcgbG9uZylhWzBdWzBdKmJbMF1bMF0rKGxvbmcgbG9uZylhWzBdWzFdKmJbMV1bMF0rKGxvbmcgbG9uZylhWzBdWzJdKmJbMl1bMF07CiAgICBxPShsb25nIGxvbmcpYVsxXVswXSpiWzBdWzBdKyhsb25nIGxvbmcpYVsxXVsxXSpiWzFdWzBdKyhsb25nIGxvbmcpYVsxXVsyXSpiWzJdWzBdOwogICAgcj0obG9uZyBsb25nKWFbMl1bMF0qYlswXVswXSsobG9uZyBsb25nKWFbMl1bMV0qYlsxXVswXSsobG9uZyBsb25nKWFbMl1bMl0qYlsyXVswXTsKCiAgICBzPShsb25nIGxvbmcpYVswXVswXSpiWzBdWzFdKyhsb25nIGxvbmcpYVswXVsxXSpiWzFdWzFdKyhsb25nIGxvbmcpYVswXVsyXSpiWzJdWzFdOwogICAgdD0obG9uZyBsb25nKWFbMV1bMF0qYlswXVsxXSsobG9uZyBs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AgICAgICAgICAgIG11bHRpcGx5KGEsYik7CiAgICAgICAgICAgICAKICAgICAgICAgfSAgICAKCiAgICAgICAgIGVsc2UgewoKICAgICAgICAgICAgICAgICAgcG93ZXIoYSxiLG4+PjEpOwoKICAgICAgICAgICAgICAgICAgbXVsdGlwbHkoYSxhKTsKCiAgICAgICAgICAgICAgfQoKfQoKaW50IG1haW4oKQp7CgogICAgaW50IHQ7CiAgICBsb25nIGxvbmcgbjsKICAgIGxsIGEsYjsKCiAgICBzY2FuZigiJWQiLCZ0KTsKCiAgICB3aGlsZSh0LS0pCiAgICB7CgogICAgICAgIHNjYW5mKCIldSV1JXUlbGxkIiwmYSwmYiwmTU9ELCZuKTsKCiAgICAgICAgbGwgZlszXSx0ZW1wWzNdWzNdOwoKICAgICAgICBmWzBdPShsbClzcXJ0KDMrKGxvbmcgbG9uZylhKmIpOy8vRygwKQogICAgICAgIGZbMV09YjsvL2YoMSkKICAgICAgICBmWzJdPWE7Ly9mKDApCgogICAgICAgIGxsIHRbM11bM107CgogICAgICAgIHRbMF1bMF09dGVtcFswXVswXT0xOwogICAgICAgIHRbMF1bMV09dGVtcFswXVsxXT0xOwogICAgICAgIHRbMF1bMl09dGVtcFswXVsyXT0wOwogICAgICAgIHRbMV1bMF09dGVtcFsxXVswXT0yOwogICAgICAgIHRbMV1bMV09dGVtcFsxXVsxXT0xOwogICAgICAgIHRbMV1bMl09dGVtcFsxXVsyXT0xOwogICAgICAgIHRbMl1bMF09dGVtcFsyXVswXT0wOwogICAgICAgIHRbMl1bMV09dGVtcFsyXVsxXT0xOwogICAgICAgIHRbMl1bMl09dGVtcFsyXVsyXT0wOwoKICAgICAgICAvL2NvdXQ8PCJFeGVjdXRpbmcuLi4iPDxlbmRsOwogICAgICAgIAoKICAgICAgICBwb3dlcih0LHRlbXAsbi0xKTsKCiAgICAgICAgLy9jb3V0PDwiRXhlY3V0aW5nLi4uIjw8ZW5kbDsKCiAgICAgICAgbGwgYW5zPShsbCkoKChsb25nIGxvbmcpdFsxXVswXSpmWzBdKyhsb25nIGxvbmcpdFsxXVsxXSpmWzFdKyhsb25nIGxvbmcpdFsxXVsyXSpmWzJdKSVNT0QpOwoKICAgICAgICBwcmludGYoIiV1XG4iLChsbClhbnMpOwoKICAgIH0KCiAgICByZXR1cm4gMDsKCn0KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCg==