#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll MOD ,mod;
ll sum(ll A, ll B)
{
A = (A + MOD) % MOD;
B = (B + MOD) % MOD;
ll ans = (A + B) % MOD;
ans = (ans + MOD) % MOD;
return ans;
}
ll f(ll a, ll n)
{
ll res = a, ans = 0;
while (n)
{
if (n % 2)
ans = (ans + res) % MOD;
res = (res + res) % MOD;
n /= 2;
}
return ans;
}
struct matran
{
ll a[2][2];
void print()
{
for (ll i = 0; i < 2; i++)
{
for (ll j = 0; j < 2; j++)
cout << a[i][j] << " ";
cout << '\n';
}
}
};
matran mot, M;
struct matran_1
{
ll a[1][2];
void print()
{
for (ll i = 0; i < 1; i++)
{
for (ll j = 0; j < 2; j++)
cout << a[i][j] << " ";
cout << '\n';
}
}
};
matran prod(matran A, matran B)
{
matran C;
for (ll i = 0; i < 2; i++)
for (ll j = 0; j < 2; j++)
C.a[i][j] = 0;
for (ll i = 0; i < 2; i++)
{
for (ll j = 0; j < 2; j++)
{
for (ll k = 0; k < 2; k++)
C.a[i][j] = sum(C.a[i][j], f(A.a[i][k], B.a[k][j]));
}
}
return C;
}
matran_1 prod1(matran_1 A, matran B)
{
matran_1 C;
for (ll i = 0; i < 1; i++)
for (ll j = 0; j < 2; j++)
C.a[i][j] = 0;
for (ll i = 0; i < 1; i++)
for (ll j = 0; j < 2; j++)
for (ll k = 0; k < 2; k++)
C.a[i][j] = sum(C.a[i][j], f(A.a[i][k], B.a[k][j]));
return C;
}
matran po(matran A, ll n)
{
matran res = A, ans = mot;
while (n)
{
if (n % 2)
ans = prod(ans, res);
res = prod(res, res);
n /= 2;
}
return ans;
}
int main()
{
ll testcase;
cin >> testcase;
while (testcase--)
{
ll a,b,m,k;
cin>>a>>b>>m>>k;
mod = MOD = m;
a%=mod;
b%=mod;
ll unit[2][2] = {{1, 0}, {0, 1}};
ll MM[2][2] = {{a,0}, {b,1}};
for (ll i = 0; i < 2; i++)
{
for (ll j = 0; j < 2; j++)
{
mot.a[i][j] = unit[i][j];
M.a[i][j] = MM[i][j];
}
}
ll tmp[1][2] = {{b, 1}};
matran_1 initi;
for (ll i = 0; i < 1; i++)
{
for (ll j = 0; j < 2; j++)
initi.a[i][j] = tmp[i][j];
}
matran_1 lala = prod1(initi, po(M, k-1));
cout << lala.a[0][0] << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmxsIE1PRCAsbW9kOwpsbCBzdW0obGwgQSwgbGwgQikKewogICAgICAgIEEgPSAoQSArIE1PRCkgJSBNT0Q7CiAgICAgICAgQiA9IChCICsgTU9EKSAlIE1PRDsKICAgICAgICBsbCBhbnMgPSAoQSArIEIpICUgTU9EOwogICAgICAgIGFucyA9IChhbnMgKyBNT0QpICUgTU9EOwogICAgICAgIHJldHVybiBhbnM7Cn0KbGwgZihsbCBhLCBsbCBuKQp7CiAgICAgICAgbGwgcmVzID0gYSwgYW5zID0gMDsKICAgICAgICB3aGlsZSAobikKICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAobiAlIDIpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucyA9IChhbnMgKyByZXMpICUgTU9EOwogICAgICAgICAgICAgICAgcmVzID0gKHJlcyArIHJlcykgJSBNT0Q7CiAgICAgICAgICAgICAgICBuIC89IDI7CiAgICAgICAgfQogICAgICAgIHJldHVybiBhbnM7Cn0Kc3RydWN0IG1hdHJhbgp7CiAgICAgICAgbGwgYVsyXVsyXTsKICAgICAgICB2b2lkIHByaW50KCkKICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgMjsgaSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgMjsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgYVtpXVtqXSA8PCAiICI7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgJ1xuJzsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9Cn07Cm1hdHJhbiBtb3QsIE07CnN0cnVjdCBtYXRyYW5fMQp7CiAgICAgICAgbGwgYVsxXVsyXTsKICAgICAgICB2b2lkIHByaW50KCkKICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgMTsgaSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgMjsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgYVtpXVtqXSA8PCAiICI7CiAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgJ1xuJzsKICAgICAgICAgICAgICAgIH0KICAgICAgICB9Cn07Cm1hdHJhbiBwcm9kKG1hdHJhbiBBLCBtYXRyYW4gQikKewogICAgICAgIG1hdHJhbiBDOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCAyOyBpKyspCiAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgMjsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICBDLmFbaV1bal0gPSAwOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCAyOyBpKyspCiAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChsbCBqID0gMDsgaiA8IDI7IGorKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChsbCBrID0gMDsgayA8IDI7IGsrKykKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBDLmFbaV1bal0gPSBzdW0oQy5hW2ldW2pdLCBmKEEuYVtpXVtrXSwgQi5hW2tdW2pdKSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiBDOwp9Cm1hdHJhbl8xIHByb2QxKG1hdHJhbl8xIEEsIG1hdHJhbiBCKQp7CiAgICAgICAgbWF0cmFuXzEgQzsKICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgMTsgaSsrKQogICAgICAgICAgICAgICAgZm9yIChsbCBqID0gMDsgaiA8IDI7IGorKykKICAgICAgICAgICAgICAgICAgICAgICAgQy5hW2ldW2pdID0gMDsKICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgMTsgaSsrKQogICAgICAgICAgICAgICAgZm9yIChsbCBqID0gMDsgaiA8IDI7IGorKykKICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChsbCBrID0gMDsgayA8IDI7IGsrKykKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBDLmFbaV1bal0gPSBzdW0oQy5hW2ldW2pdLCBmKEEuYVtpXVtrXSwgQi5hW2tdW2pdKSk7CiAgICAgICAgcmV0dXJuIEM7Cn0KbWF0cmFuIHBvKG1hdHJhbiBBLCBsbCBuKQp7CiAgICAgICAgbWF0cmFuIHJlcyA9IEEsIGFucyA9IG1vdDsKICAgICAgICB3aGlsZSAobikKICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAobiAlIDIpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucyA9IHByb2QoYW5zLCByZXMpOwogICAgICAgICAgICAgICAgcmVzID0gcHJvZChyZXMsIHJlcyk7CiAgICAgICAgICAgICAgICBuIC89IDI7CiAgICAgICAgfQogICAgICAgIHJldHVybiBhbnM7Cn0KaW50IG1haW4oKQp7CiAgICAgCgogICAgICAgIGxsIHRlc3RjYXNlOwogICAgICAgIGNpbiA+PiB0ZXN0Y2FzZTsKICAgICAgICB3aGlsZSAodGVzdGNhc2UtLSkKICAgICAgICB7CiAgICAgICAgICAgICAgICBsbCBhLGIsbSxrOwogICAgICAgICAgICAgICAgY2luPj5hPj5iPj5tPj5rOwogICAgICAgICAgICAgICAgbW9kID0gTU9EID0gbTsKICAgICAgICAgICAgICAgIGElPW1vZDsKICAgICAgICAgICAgICAgIGIlPW1vZDsKICAgICAgICAgICAgICAgIGxsIHVuaXRbMl1bMl0gPSB7ezEsIDB9LCB7MCwgMX19OwogICAgICAgICAgICAgICAgbGwgTU1bMl1bMl0gPSB7e2EsMH0sIHtiLDF9fTsKCiAgICAgICAgICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgMjsgaSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgMjsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbW90LmFbaV1bal0gPSB1bml0W2ldW2pdOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIE0uYVtpXVtqXSA9IE1NW2ldW2pdOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBsbCB0bXBbMV1bMl0gPSB7e2IsIDF9fTsKICAgICAgICAgICAgICAgIG1hdHJhbl8xIGluaXRpOwogICAgICAgICAgICAgICAgZm9yIChsbCBpID0gMDsgaSA8IDE7IGkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChsbCBqID0gMDsgaiA8IDI7IGorKykKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbml0aS5hW2ldW2pdID0gdG1wW2ldW2pdOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgbWF0cmFuXzEgbGFsYSA9IHByb2QxKGluaXRpLCBwbyhNLCBrLTEpKTsKICAgICAgICAgICAgICAgIGNvdXQgPDwgbGFsYS5hWzBdWzBdIDw8ICdcbic7CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwp9Cg==