#include <bits/stdc++.h>
#define fi first
#define se second
#define REP(i, a, b) for (int i = (a); i <= (b); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = (a); i > (b); --i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define endl '\n'
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define unmap unordered_map
#define pq priority_queue
#define minEle min_element
#define maxEle max_element
#define lb lower_bound //first pos >= val
#define ub upper_bound // first pos > val
#define bp __builtin_popcount
#define debug(a) cout << a << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <ll, ld> pld;
typedef pair <ll, pll> pllp;
typedef pair <pll, ll> ppll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
typedef map <int, int> mii;
typedef map <ll, ll> mll;
typedef string BigNum;
const ll maxN = 1e18;
const ll minN = -1e18;
const int INF = 2e9;
const ll MOD = 1e9 + 7;
const ll MOD1 = 998244353;
const int baseHash = 331;
const int bigNumLength = 5000;
const ld PI = acos(-1);
//Remember limit for IT, etc..., and +1 in limit of any array
const ll limit = 2e5 + 5;
const ll limit1 = 1e6 + 5;
const ll limit2 = 1e3 + 5;
//If TLE let's use int instead of ll because it's as slow as your WPM :)))
/*----IMPORTANT THINGS----*/
pii dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
//right down left up rightdown leftdown rightup leftup
pii NON = {-1, -1};
/*------------------------*/
int t, a2, n, mod;
struct Matrix {
vector <vi> c;
int rows, cols;
Matrix(int rows, int cols) : rows(rows), cols(cols) {
c.resize(rows);
FOR(i, 0, rows) c[i].resize(cols);
}
Matrix operator * (const Matrix &other) {
Matrix ans(rows, other.cols);
FOR(i, 0, rows) {
FOR(j, 0, other.cols) {
ans.c[i][j] = 0;
FOR(k, 0, cols) {
ans.c[i][j] += (1LL * c[i][k] * other.c[k][j]) % mod;
ans.c[i][j] %= mod;
}
}
}
return ans;
}
Matrix power(const ll &n) {
if (n == 1) return (*this);
Matrix ans = power(n >> 1);
return (n & 1) ? ans * ans * (*this) : ans * ans;
}
};
void solveProblem() {
Matrix ans(4, 4);
ans.c[0][0] = 1, ans.c[0][1] = 0, ans.c[0][2] = 0, ans.c[0][3] = 0;
ans.c[1][0] = (4LL * a2 * a2) % mod, ans.c[1][1] = (4LL * a2 * a2) % mod, ans.c[1][2] = 1, ans.c[1][3] = 2LL * a2 % mod;
ans.c[2][0] = 1, ans.c[2][1] = 1, ans.c[2][2] = 0, ans.c[2][3] = 0;
ans.c[3][0] = (mod + (-4LL * a2 % mod)); ans.c[3][1] = (mod + (-4LL * a2 % mod)); ans.c[3][2] = 0, ans.c[3][3] = mod - 1;
if (n <= 2) {
cout << (n == 1 ? 1 % mod : (1LL + 1LL * a2 * a2) % mod) << endl;
return;
}
ans = ans.power(n - 2);
int res = 0;
int sum12 = (1LL + 1LL * a2 * a2) % mod;
(res += (1LL * sum12 * ans.c[0][0]) % mod) %= mod;
(res += 1LL * ans.c[1][0] * a2 % mod * a2 % mod) %= mod;
(res += 1LL * ans.c[2][0]) %= mod;
(res += 1LL * ans.c[3][0] * a2 % mod) %= mod;
cout << (res + mod) % mod << endl;
}
void fastInput() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void readInput() {
cin >> t;
while (t--) {
cin >> a2 >> n >> mod;
solveProblem();
}
}
int main() {
fastInput();
readInput();
// solveProblem();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgZmkgZmlyc3QKI2RlZmluZSBzZSBzZWNvbmQKI2RlZmluZSBSR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pdCdzIGFzIHNsb3cgYXMgeW91ciBXUE0gOikpKQoKLyotLS0tSU1QT1JUQU5UIFRISU5HUy0tLS0qLwpwaWkgZGlyW10gPSB7ezAsIDF9LCB7MSwgMH0sIHswLCAtMX0sIHstMSwgMH0sIHsxLCAxfSwgezEsIC0xfSwgey0xLCAxfSwgey0xLCAtMX19OwovL3JpZ2h0IGRvd24gbGVmdCB1cCByaWdodGRvd24gbGVmdGRvd24gcmlnaHR1cCBsZWZ0dXAKcGlpIE5PTiA9IHstMSwgLTF9OwovKi0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSovCgppbnQgdCwgYTIsIG4sIG1vZDsKCnN0cnVjdCBNYXRyaXggewogICAgdmVjdG9yIDx2aT4gYzsKICAgIGludCByb3dzLCBjb2xzOwoKICAgIE1hdHJpeChpbnQgcm93cywgaW50IGNvbHMpIDogcm93cyhyb3dzKSwgY29scyhjb2xzKSB7CiAgICAgICAgYy5yZXNpemUocm93cyk7CiAgICAgICAgRk9SKGksIDAsIHJvd3MpIGNbaV0ucmVzaXplKGNvbHMpOwogICAgfQoKICAgIE1hdHJpeCBvcGVyYXRvciAqIChjb25zdCBNYXRyaXggJm90aGVyKSB7CiAgICAgICAgTWF0cml4IGFucyhyb3dzLCBvdGhlci5jb2xzKTsKCiAgICAgICAgRk9SKGksIDAsIHJvd3MpIHsKICAgICAgICAgICAgRk9SKGosIDAsIG90aGVyLmNvbHMpIHsKICAgICAgICAgICAgICAgIGFucy5jW2ldW2pdID0gMDsKCiAgICAgICAgICAgICAgICBGT1IoaywgMCwgY29scykgewogICAgICAgICAgICAgICAgICAgIGFucy5jW2ldW2pdICs9ICgxTEwgKiBjW2ldW2tdICogb3RoZXIuY1trXVtqXSkgJSBtb2Q7CiAgICAgICAgICAgICAgICAgICAgYW5zLmNbaV1bal0gJT0gbW9kOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICByZXR1cm4gYW5zOwogICAgfQoKICAgI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