#pragma GCC optimize ("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include"iostream"
#include"cassert"
#include"cmath"
#include"cstdio"
#include"cstdlib"
#include"map"
#include"set"
#include"queue"
#include"vector"
#include"algorithm"
#include"numeric"
#include"stack"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
inline int in(){int x;scanf("%lld",&x);return x;}
const int mod=1e9+7;
const int N=2e5+10;
int f[N];
int modp(int a,int b){
	if(b==0)return 1;
	int u=modp(a,b/2);
	u=(u*u)%mod;
	if(b&1)return (a*u)%mod;
	return u;
}
inline int inv(int x){return modp(x,mod-2);}
inline int ncr(int n,int r){
	if(n<r)return 0;
	int res=(f[n]*inv(f[n-r]))%mod;
	return (res*inv(f[r]))%mod;
}
void solve(){
	int n=in();int k=in();
	int ans=1;
	int suf=n;
	for(int p=1;p<=n;p++){

		int num=(f[p]*ncr(n-(k-1)*(p-1), p))%mod;
		num*=inv(suf);
		num%=mod;
		suf=suf*(n-p)%mod;
		ans=(ans+num)%mod;
	}
	cout<<ans<<"\n";
}
int32_t main()
{

	//Explanation : https://codeforces.com/blog/entry/91271?#comment-798021

	f[0]=1;
	for(int i=1;i<N;i++)f[i]=f[i-1]*i%mod;
	int t=in();
	while(t--)solve();

}

