#include <fstream>
using namespace std;
ifstream cin("piggy.in");
ofstream cout("piggy.out");
int zhu,fang,f[1000001],ans=0;
void dfs(int step)
{
int r,a;
if (step>zhu)
{
a=0;
for (r=1;r<=fang;r++)
if (f[r]==0) a=1;
if (a==0) ans++;
}else
{
for (r=1;r<=fang;r++)
{
f[r]++;
dfs(step+1);
f[r]--;
if (f[1]==0) {cout<<ans/(fang-1)<<endl;ans=-1;return;}
}
}
}
int main()
{
int t,i,j;
cin>>t;
for (i=1;i<=t;i++)
{
cin>>zhu>>fang;
if (fang==1) cout<<1<<endl;else
if (zhu<fang) cout<<0<<endl;else
{
for (j=0;j<=zhu;j++) f[j]=0;
ans=0;dfs(1);
if (ans!=-1) cout<<ans/(fang-1)<<endl;
}
}
return 0;
}