/***************************************************************** Author:lbn187, a konjac TwT Date:2016-4-16 Contest:Bestcoder Algorithm:QAQ ^_^ Orz hzh, NOI gold members, IOI 2016 world champion ^=^ Orz hhw, you are our blue moon, with you will live ^~^ Orz mxh, you are our red sun, without you we'll die ^-^ Orz yizhou, I wish you a successful marriage ^w^ Orz xianyangyu, you can sleep all the time in the game *****************************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include #include #include #include #include #include #include #include #define sf scanf #define pf printf #define fo(i,n) for(int i=0;i=0;i--) #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,a,n) for(int i=n;i>=a;i--) #define mjj(i,x) for(int i=fir[x];i;i=ne[i]) #define X first #define Y second #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) #define mp make_pair #define rlr son[son[root][1]][0] #define gi(a) sf("%d",&a); #define gi2(a,b) sf("%d%d",&a,&b); #define gi3(a,b,c) sf("%d%d%d",&a,&b,&c); #define gl(a) sf("%I64d",&a); #define gs(s) sf("%s",s); #define pi(a) pf("%d\n",a); #define pi2(a,b) pf("%d %d\n",a,b); #define pl(a) pf("%I64d\n",a); #define spr(x) (x*x) #define pie acos(-1) #define CM(a,b) memcpy(a,b,sizeof(b)) #define CL(a) memset(a,0,sizeof(a)) #define N 222222 #define M 1000000007 #define inf 1e9 #define eps 1e-8 using namespace std; typedef long long LL;typedef unsigned long long ULL;typedef double LF;typedef long double LD; typedef pairpii;typedef pairpil;typedef pairpli;typedef pairpll;typedef pairpff; const int xx[]={1,-1,0,0},yy[]={0,0,1,-1};LL pow(LL a,LL b,LL p){LL sum=1;for(a%=p;b;a=a*a%p,b>>=1)if(b&1)sum=sum*a%p;return sum;} char ch;void rd(int &x){for(ch=getchar();ch<'0';ch=getchar());for(x=0;ch>='0';ch=getchar())x=x*10+ch-'0';} LL phi(LL n){LL i,re=n;for(i=2;i*i<=n;i++)if(n%i==0){re=re/i*(i-1);while(n%i==0)n/=i;}if(n>1)re=re/n*(n-1);return re%M;} void exgcd(LL a,LL b,LL &x,LL &y){if(!b){x=1;y=0;return;}exgcd(b,a%b,y,x);y-=x*(a/b);} LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);} int T,n,m,x,y,i,j,tot,cnt,a[N];LL A,B,C,P,ans; LL mul(LL a,LL b,LL p){ LL sum=0;for(;b;a=(a+a)%p,b>>=1)if(b&1)sum=(sum+a)%p; return sum; } int main(){ for(scanf("%d",&T);T--;){ scanf("%lld%lld",&A,&P); if(A<=2){puts("0");continue;} A-=2; if(A%2==0)printf("%lld\n",mul(A/2,A+1,P)); else printf("%lld\n",mul(A,(A+1)/2,P)); //fr(i,n)gi(a[i]); } }