#include #include #ifdef LOCAL #include #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif using namespace atcoder; using mint=modint998244353; using namespace std; using ll=long long; using ul=unsigned long long; int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; using Graph=vector>; ll op(ll a,ll b){return min(a,b);} ll e(){return 2e9;} ll inv(ll a,ll m){ ll mod=m,u=1,v=0; while(mod){ ll t=a/mod; a-=t*mod;swap(a,mod); u-=t*v;swap(u,v); } u%=m; if(u<0)u+=m; return u; } int main(){ int T; cin>>T; while(T--){ ll A,P; cin>>A>>P; bool ok=false; if(P==1){cout<<-1<>0)&1)ans*=cnt,ans%=P; t=t>>1; cnt*=cnt; } cout<