#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const int block=1e3+5;
typedef long long LL;
const int mod=1e9+7;
LL inv[maxn],sum[block][maxn],c[block][block];
LL pows(LL a,int b)
{
LL res=1;
while(b)
{
if(b&1)res=res*a%mod;
a=a*a%mod;
b/=2;
}
return res%mod;
}
void init()
{
for(int i=1;i<maxn;i++)
inv[i]=pows(i,mod-2);
for(int i=0;i<=100;i++)
{
LL pre=1;
sum[i][0]=1;
int cur=i*1000;
for(int j=1;j<=1e5;j++)
{
if(j>cur)
sum[i][j]=sum[i][j-1];
else
{
pre=(pre*(cur-j+1)%mod)*inv[j]%mod;
sum[i][j]=(sum[i][j-1]+pre)%mod;
}
}
}
for(int i=0;i<=1000;i++)
{
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
}
int main()
{
int T;
scanf("%d",&T);
init();
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
int cur=n/1000;
int t=n%1000;
LL ans=0;
int cnt=0;
for(int i=m;i>=0;i--)
{
LL tmp1=c[t][cnt]*sum[cur][i]%mod;
ans=(ans+tmp1)%mod;
cnt++;
if(cnt>t)
break;
}
printf("%lld\n",ans);
}
}