#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mod, m;
struct matran
{
ll a[4][4];
void print()
{
for (ll i = 0; i < 4; i++)
{
for (ll j = 0; j < 4; j++)
{
cout << a[i][j] << " ";
}
cout << '\n';
}
}
};
struct matran_1
{
ll a[1][4];
void print()
{
for (ll i = 0; i < 1; i++)
{
for (ll j = 0; j < 4; j++)
{
cout << a[i][j] << " ";
}
cout << '\n';
}
}
};
matran M, mot;
matran_1 initi;
matran prod(matran A, matran B)
{
matran C;
for (ll i = 0; i < 4; i++)
for (ll j = 0; j < 4; j++)
C.a[i][j] = 0;
for (ll i = 0; i < 4; i++)
for (ll j = 0; j < 4; j++)
for (ll k = 0; k < 4; k++)
C.a[i][j] = (C.a[i][j] + (A.a[i][k] * B.a[k][j]) % mod) % mod;
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 < 4; j++)
C.a[i][j] = 0;
for (ll i = 0; i < 1; i++)
for (ll j = 0; j < 4; j++)
for (ll k = 0; k < 4; k++)
C.a[i][j] = (C.a[i][j] + (A.a[i][k] * B.a[k][j]) % mod) % mod;
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;
}
void solve()
{
ll a_2, n, m;
cin >> a_2 >> n >> m;
mod = m;
a_2 %= mod;
ll g_2 = (1+a_2)%mod;
ll am_1 = ((-1)*(4*a_2%mod)+mod)%mod;
ll duong_2 = ((1)*(4*a_2%mod*a_2%mod)+mod)%mod;
ll unit[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0},{0,0,1,0},{0,0,0,1}};
ll tmp[4][4] = {{1,0,0,0},{1,duong_2,(2*a_2%mod),1},{0,am_1,(-1+mod)%mod,0},{0,1,0,0}};
for (ll i = 0; i < 4; i++)
for (ll j = 0; j < 4; j++)
{
mot.a[i][j] = unit[i][j];
M.a[i][j] = tmp[i][j];
}
initi.a[0][0] = 1;
initi.a[0][1] = (a_2*a_2%mod);
initi.a[0][2] = (a_2%mod);
initi.a[0][3]= 1;
matran_1 ress;
ress = prod1(initi, po(M, n - 1));
cout << ress.a[0][0] << '\n';
}
int main()
{
ll testcase;
cin >> testcase;
while (testcase--)
solve();
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmxsIG1vZCwgbTsKc3RydWN0IG1hdHJhbgp7CiAgICAgICAgbGwgYVs0XVs0XTsKICAgICAgICB2b2lkIHByaW50KCkKICAgICAgICB7CiAgICAgICAgICAgICAgICBmb3IgKGxsIGkgPSAwOyBpIDwgNDsgaSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCBhW2ldW2pdIDw8ICIgIjsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBjb3V0IDw8ICdcbic7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgfQp9OwpzdHJ1Y3QgbWF0cmFuXzEKewogICAgICAgIGxsIGFbMV1bNF07CiAgICAgICAgdm9pZCBwcmludCgpCiAgICAgICAgewogICAgICAgICAgICAgICAgZm9yIChsbCBpID0gMDsgaSA8IDE7IGkrKykKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChsbCBqID0gMDsgaiA8IDQ7IGorKykKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgYVtpXVtqXSA8PCAiICI7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgY291dCA8PCAnXG4nOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KfTsKbWF0cmFuIE0sIG1vdDsKbWF0cmFuXzEgaW5pdGk7Cm1hdHJhbiBwcm9kKG1hdHJhbiBBLCBtYXRyYW4gQikKewogICAgICAgIG1hdHJhbiBDOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCA0OyBpKyspCiAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICBDLmFbaV1bal0gPSAwOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCA0OyBpKyspCiAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGsgPSAwOyBrIDwgNDsgaysrKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEMuYVtpXVtqXSA9IChDLmFbaV1bal0gKyAoQS5hW2ldW2tdICogQi5hW2tdW2pdKSAlIG1vZCkgJSBtb2Q7CiAgICAgICAgcmV0dXJuIEM7Cn0KbWF0cmFuXzEgcHJvZDEobWF0cmFuXzEgQSwgbWF0cmFuIEIpCnsKICAgICAgICBtYXRyYW5fMSBDOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCAxOyBpKyspCiAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICBDLmFbaV1bal0gPSAwOwogICAgICAgIGZvciAobGwgaSA9IDA7IGkgPCAxOyBpKyspCiAgICAgICAgICAgICAgICBmb3IgKGxsIGogPSAwOyBqIDwgNDsgaisrKQogICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGxsIGsgPSAwOyBrIDwgNDsgaysrKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIEMuYVtpXVtqXSA9IChDLmFbaV1bal0gKyAoQS5hW2ldW2tdICogQi5hW2tdW2pdKSAlIG1vZCkgJSBtb2Q7CiAgICAgICAgcmV0dXJuIEM7Cn0KbWF0cmFuIHBvKG1hdHJhbiBBLCBsbCBuKQp7CiAgICAgICAgbWF0cmFuIHJlcyA9IEEsIGFucyA9IG1vdDsKICAgICAgICB3aGlsZSAobikKICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAobiAlIDIpCiAgICAgICAgICAgICAgICAgICAgICAgIGFucyA9IHByb2QoYW5zLCByZXMpOwogICAgICAgICAgICAgICAgcmVzID0gcHJvZChyZXMsIHJlcyk7CiAgICAgICAgICAgICAgICBuIC89IDI7CiAgICAgICAgfQogICAgICAgIHJldHVybiBhbnM7Cn0Kdm9pZCBzb2x2ZSgpCnsKICAgICAgICBsbCBhXzIsIG4sIG07CiAgICAgICAgY2luID4+IGFfMiA+PiBuID4+IG07CiAgICAgICAgbW9kID0gbTsKICAgICAgICBhXzIgJT0gbW9kOwogICAgICAgIGxsIGdfMiA9ICgxK2FfMiklbW9kOwogICAgICAgIGxsIGFtXzEgPSAoKC0xKSooNCphXzIlbW9kKSttb2QpJW1vZDsKICAgICAgICBsbCBkdW9uZ18yID0gKCgxKSooNCphXzIlbW9kKmFfMiVtb2QpK21vZCklbW9kOwogICAgICAgIGxsIHVuaXRbNF1bNF0gPSB7ezEsIDAsIDAsIDB9LCB7MCwgMSwgMCwgMH0sezAsMCwxLDB9LHswLDAsMCwxfX07CiAgICAgICAgbGwgdG1wWzRdWzRdID0ge3sxLDAsMCwwfSx7MSxkdW9uZ18yLCgyKmFfMiVtb2QpLDF9LHswLGFtXzEsKC0xK21vZCklbW9kLDB9LHswLDEsMCwwfX07CiAgICAgICAgZm9yIChsbCBpID0gMDsgaSA8IDQ7IGkrKykKICAgICAgICAgICAgICAgIGZvciAobGwgaiA9IDA7IGogPCA0OyBqKyspCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIG1vdC5hW2ldW2pdID0gdW5pdFtpXVtqXTsKICAgICAgICAgICAgICAgICAgICAgICAgTS5hW2ldW2pdID0gdG1wW2ldW2pdOwogICAgICAgICAgICAgICAgfQogICAgICAgIGluaXRpLmFbMF1bMF0gPSAxOwogICAgICAgIGluaXRpLmFbMF1bMV0gPSAoYV8yKmFfMiVtb2QpOwogICAgICAgIGluaXRpLmFbMF1bMl0gPSAoYV8yJW1vZCk7CiAgICAgICAgaW5pdGkuYVswXVszXT0gMTsKICAgICAgICAKICAgICAgICBtYXRyYW5fMSByZXNzOwogICAgICAgIHJlc3MgPSBwcm9kMShpbml0aSwgcG8oTSwgbiAtIDEpKTsKICAgICAgICBjb3V0IDw8IHJlc3MuYVswXVswXSA8PCAnXG4nOwogICAgIAp9CmludCBtYWluKCkKewogICAKCiAgICAgICAgbGwgdGVzdGNhc2U7CiAgICAgICAgY2luID4+IHRlc3RjYXNlOwogICAgICAgIHdoaWxlICh0ZXN0Y2FzZS0tKQogICAgICAgICAgICAgICAgc29sdmUoKTsKICAgICAgICByZXR1cm4gMDsKfQ==