#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define bug(x,y) cout<<"x = "<<x<<" y = "<<y<<endl;
#define yo cout<<"ok i reached here\n";
using namespace std;
const int mod = 1e9+7,siz = 1e6+5;
int t,n,m,k,a[siz],fact[siz];
int bix(int a,int n)
{
    int ret = 1;
    while(n>0)
    {
        if(n&1) (ret*=a)%=mod;
        (a*=a)%=mod; n/=2;
    }
    return ret;
}
int div(int x)
{
    int ret = bix(x,mod-2);
    return ret;
}
void pre()
{
    fact[0] = 1;
    for(int i = 1;i<siz;i++) fact[i] = (fact[i-1]*i)%mod;
}
int ncr(int n,int r)
{
    if(r>n or n<0) return 0;
    if(n == 0 and r == 0) return 1;
    int ret = fact[n];
    (ret*=bix(fact[r],mod-2))%=mod;
    (ret*=bix(fact[n-r],mod-2))%=mod;
    return ret;
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);

    pre();
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        int ans = 1;
        for(int i = 1;i<=n;i++)
        {
            int space = n - 1LL*(k-1)*(i-1);
            int prob = ncr(space,i); prob*=bix(ncr(n,i),mod-2); prob%=mod;
            (ans+=prob)%=mod;
        }
        cout<<ans<<endl;
    }

}
