// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e5 + 6, M = 1e9 + 7;
ll n, k, f[N] = {1}, inf[N] = {1};
ll in(ll a, ll b = M - 2, ll m = M)
{
    ll r = 1;
    while (b)
    {
        if (b % 2)
            r = r * a % m;
        a = a * a % m;
        b /= 2;
    }
    return r;
}
ll t(ll x)
{
    return f[n] * inf[n - x] % M;
}
ll d(ll x)
{
    ll r = n - x - (k - 1) * (x - 1);
    if (r < 0)
        return 0;
    return f[x + r] * inf[r] % M;
}
int main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    for (ll i = 1; i < N; i++)
        f[i] = f[i - 1] * i % M, inf[i] = in(f[i]);
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> k;
        ll p = 0;
        for (ll x = 2; x <= n; x++)
        {
            ll a = (t(x) - d(x) + M) % M;
            a -= (t(x - 1) - d(x - 1) + M) % M * (n - x + 1) % M;
            a += M;
            a %= M;
            p += x * a % M * in(t(x)) % M;
            p %= M;
        }
        cout << p << '\n';
    }
}