#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgc29sdmVfdGVzdF9jYXNlKCkgewogICAgaW50IG4sIGs7CiAgICBjaW4gPj4gbiA+PiBrOwogICAgCiAgICB2ZWN0b3I8aW50PiBkaWZmaWN1bHRpZXMobik7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNpbiA+PiBkaWZmaWN1bHRpZXNbaV07CiAgICB9CiAgICAKICAgIC8vIFNvcnQgZGlmZmljdWx0aWVzIGluIGFzY2VuZGluZyBvcmRlcgogICAgc29ydChkaWZmaWN1bHRpZXMuYmVnaW4oKSwgZGlmZmljdWx0aWVzLmVuZCgpKTsKICAgIAogICAgLy8gRmluZCB0aGUgbWF4aW11bSBudW1iZXIgb2YgcHJvYmxlbXMgdGhhdCBjYW4gYmUgaW5jbHVkZWQKICAgIGludCBtYXhfcHJvYmxlbXMgPSAxOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICBpbnQgaiA9IGk7CiAgICAgICAgd2hpbGUgKGogPCBuICYmIGRpZmZpY3VsdGllc1tqXSAtIGRpZmZpY3VsdGllc1tpXSA8PS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