#include<bits/stdc++.h>
#define llong long long
#define mkpr make_pair
#define x first
#define y second
#define iter iterator
#define riter reverse_iterator
#define y1 Lorem_ipsum_
#define tm dolor_sit_amet_
using namespace std;

inline int read()
{
	int x = 0,f = 1; char ch = getchar();
	for(;!isdigit(ch);ch=getchar()) {if(ch=='-') f = -1;}
	for(; isdigit(ch);ch=getchar()) {x = x*10+ch-48;}
	return x*f;
}

const int P = 1e9+7;
const int mxN = 2e5;
int n,m; llong fact[mxN+3],facti[mxN+3],inv[mxN+3];

llong quickpow(llong x,llong y)
{
	llong cur = x,ret = 1ll;
	for(int i=0; y; i++)
	{
		if(y&(1ll<<i)) {y-=(1ll<<i); ret = ret*cur%P;}
		cur = cur*cur%P;
	}
	return ret;
}
llong comb(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:fact[x]*facti[y]%P*facti[x-y]%P;}
llong combi(llong x,llong y) {return x<0ll||y<0ll||x<y?0ll:facti[x]*fact[y]%P*fact[x-y]%P;}

void initfact(int n)
{
	fact[0] = 1ll; for(int i=1; i<=n; i++) fact[i] = fact[i-1]*i%P;
	facti[n] = quickpow(fact[n],P-2); for(int i=n-1; i>=0; i--) facti[i] = facti[i+1]*(i+1ll)%P;
}

int main()
{
	initfact(mxN);
	int T = read(); while(T--)
	{
		n = read(),m = read(); llong ans = 0ll;
		for(int i=0; i<=n; i++)
		{
			llong cur = comb(n-1ll*(i-1)*(m-1),i)*combi(n,i)%P;
			ans = (ans+cur)%P;
		}
		printf("%lld\n",ans);
	}
	return 0;
}