#include<iostream>
#include<cstdio>
using namespace std;
int f[2003][2003];
int a[2005][2005];
int n,m,t,k;
int main()
{
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
int i,j;
scanf("%d%d",&t,&k);
for (i=1;i<=2000;i++)
a[i][0]=a[0][i]=1;
for (i=1;i<=2000;i++)
for (j=1;j<=i;j++)
{
a[i][j]=(a[i-1][j]+a[i-1][j-1])%k;
if (!a[i][j]) f[i][j]=1;
}
for (i=1;i<=2000;i++)
for (j=1;j<=2000;j++)
f[i][j]=f[i][j]+f[i-1][j]+f[i][j-1]-f[i-1][j-1];
for (i=1;i<=t;i++)
{
scanf("%d%d",&n,&m);
printf("%d\n",f[n][m]);
}
}