#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long int MOD = 1000000007;
long long int fact[MAXN], inv_fact[MAXN];
long long int power(long long int a, long long int b)
{
	if (!b)
		return 1;
	long long int ans = power(a, b/2);
	ans = (ans * ans) % MOD;
	if (b % 2)
		ans = (ans * a) % MOD;
	return ans;
}
void precompute()
{
	fact[0] = 1;
	for (int i = 1; i < MAXN; ++i)
	{
		fact[i] = (fact[i - 1] * i) % MOD;
	}

	inv_fact[MAXN - 1] = power(fact[MAXN - 1], MOD - 2);
	for (int i = MAXN - 2; i >= 0; --i)
	{
		inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
	}
}
long long int comb(long long int n, long long int r)
{
	if (n < 0 || r < 0 || n < r)
		return 0;

	long long int ans = fact[n];
	ans = (ans * inv_fact[r]) % MOD;
	ans = (ans * inv_fact[n - r]) % MOD;
	return ans;
}
int main(int argc, char const *argv[])
{
	precompute();
	int t;
	cin>>t;
	while (t--)
	{
		long long int n, k;
		cin>>n>>k;

		long long int ans = 1;
		for (long long int i = 1; i <= n; ++i)
		{
			long long int ways = (fact[i] * comb(n - ((i - 1) * (k - 1)), i)) % MOD;
			long long int prob = (inv_fact[n] * fact[n - i]) % MOD;
			
			long long int val = (ways * prob) % MOD;
			ans = (ans + val) % MOD;
		}

		cout<<ans<<"\n";
	}
	return 0;
}