#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void solve_test_case() {
    int n, k;
    cin >> n >> k;
    
    vector<int> difficulties(n);
    for (int i = 0; i < n; i++) {
        cin >> difficulties[i];
    }
    
    // Sort difficulties in ascending order
    sort(difficulties.begin(), difficulties.end());
    
    // Find the maximum number of problems that can be included
    int max_problems = 1;
    for (int i = 0; i < n; i++) {
        int j = i;
        while (j < n && difficulties[j] - difficulties[i] <= k) {
            j++;
        }
        max_problems = max(max_problems, j - i);
    }
    
    // The answer is the minimum number to remove
    cout << n - max_problems << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int t;
    cin >> t;
    
    while (t--) {
        solve_test_case();
    }
    
    return 0;
}