#include <iostream>
#include<cstdio>
#include<cstdlib>
#define maxn 2005
using namespace std;
int c[maxn][maxn];
int s[maxn][maxn];
int t,k,n,m;
int main(){
freopen("problem.in","r",stdin);
freopen("problem.out","w",stdout);
cin>>t>>k;
for (int i=1;i<maxn;i++){
c[i][0]=1;
c[i][i]=1;
for (int j=1;j<i;j++)
c[i][j]=(c[i-1][j]+c[i-1][j-1])%k;
}
for (int i=1;i<maxn;i++)
for (int j=1;j<=i;j++){
s[i][j]=s[i][j-1];
if (c[i][j]==0)
s[i][j]++;
}
for (int time=1;time<=t;time++){
cin>>n>>m;
int ans=0;
for(int i=1;i<=n;i++){
int j=min(i,m);
ans+=s[i][j];
}
cout<<ans<<endl;
}
return 0;
}