#include using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; #define rep(i,n) for(ll i=0;i T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); } template T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); } template inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; } template inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; } template ostream &operator<<(ostream &os, const vector &a){ if (a.empty()) return os; os << a.front(); for (auto e : a | views::drop(1)){ os << ' ' << e; } return os; } void dump(auto ...vs){ ((cout << vs << ' '), ...) << endl; } #include using namespace atcoder; using mint = dynamic_modint<0>; void solve() { ll N,M,L,B; cin>>N>>M>>L>>B; mint::set_mod(B); typedef array S; auto mult=[&M,L](S a,S b)->S { S c={0,0,0,0}; rep(i,4){ rep(j,4){ mint v=a[i]*b[j]; if (i&j&1){ v*=M; } if (i&j&2){ v*=L; } c[i^j]+=v; } } return c; }; S ans={1,0,0,0}; S base={0,1,1,0}; { ll bn=N; while (bn>0){ if (bn&1){ ans=mult(ans,base); } base=mult(base,base); bn>>=1; } } if (N%2==0){ cout<sync_with_stdio(0); ll T=1; cin>>T; while (T--){ solve(); } return 0; }