#include #include #include #include #include #include #include #include #include #include using namespace std; const int N=100005; const int mod=1e9+7; typedef long long LL; LL multi(LL a, LL b, LL m) { LL exp = a %m, res = 0; while (b) { if (b & 1) //b的最低位是否为1 { res = res + exp; if (res >= m) res = res -m; } exp = exp *2; if (exp > m) exp = exp - m; b >>= 1; //将b除以2 } return res; } int main() { int t,n,flag,c,flag1; scanf("%d",&t); while(t--) { long long q,p,a,b; cin>>q>>p; if(q&1) { a=(q-1)/2; b=q-2; } else { a=(q-1); b=(q-2)/2; } cout<<(multi(a,b,p))<