#include <iostream>
using namespace std;

const int MOD = 1e9+7, N = 1e5+5;

long long inv(long long a) {
    long long b = 1;
    int k = MOD-2;
    while(k) {
        if(k%2) b = b * a % MOD;
        a = a * a % MOD;
        k /= 2;
    }
    return b;
}

long long fact[N], ifact[N];

long long binom(long long x, long long y) {
    if(x < 0 || y < 0 || x < y) return 0;
    return fact[x] * ifact[x-y] % MOD * ifact[y] % MOD;
}

int t, n, k;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    fact[0] = ifact[0] = 1;
    for (int i = 1; i < N; i++) fact[i] = fact[i-1] * i % MOD;
    ifact[N-1] = inv(fact[N-1]);
    for (int i = N-2; 0 < i; i--) ifact[i] = ifact[i+1] * (i+1) % MOD;
    cin >> t;
    while(t--) {
        cin >> n >> k;
        long long sol = 0;
        for (long long l = 1; l <= n; l++) sol = (sol + binom(n-(l-1)*(k-1), l) * fact[l] % MOD * fact[n-l]) % MOD;
        sol = (sol + fact[n]) * ifact[n] % MOD;
        cout << sol << "\n";
    }
    return 0;
}
