#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <random>

using namespace std;

long long mod = 1e9 + 7;

long long pow(long long a, long long n) {
    if (n == 0) {
        return 1;
    }
    auto l = pow(a, n  / 2);
    return (l * l % mod * (n % 2 == 1 ? a : 1) % mod);
}

long long inv(long long a) {
    return pow(a, mod - 2);
}

vector < long long > fact(1, 1);

long long factorial(long long n) {
    while (fact.size() <= n) {
        fact.push_back(fact.back() * fact.size() % mod);
    }
    return fact[n];
}

long long C(long long n, long long k) {

    if (k > n) {
        return 0;
    }

    return (factorial(n) * inv(fact[n - k]) % mod * inv(fact[k]) % mod);

}

void solve() {
    long long n, k;
    cin>>n>>k;

    long long ans = 0;
    for (int x = 1; x <= n; x++) {
        if (n - (k - 1) * (x - 1) < x) {
            continue;
        }
        ans += C(n - (k - 1) * (x - 1), x) * inv(C(n, x));
        /*ans += C(n - (k - 1) * (x - 1), x) * factorial(x) % mod * factorial(n - x) % mod *
                inv(factorial(n)) % mod;*/
        ans %= mod;
    }

    //ans = ans * inv(C(n, k));
    ans += 1;
    ans %= mod;

    cout<<ans<<"\n";

}

int main() {
    /*cout<<factorial(5)<<"\n";
    cout<<C(5, 2)<<"\n";*/
    //cout<<inv(inv(3))<<"\n";
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--)
        solve();
}