#include <bits/stdc++.h>   using namespace std;   void init() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   void skipInputLine() { cin.ignore(numeric_limits<streamsize>::max(),   n ); }   template <typename TYPE>   TYPE readNum() {     TYPE item;     cin >> item;     return item;   }   template <typename COLLECTION>   COLLECTION readNums(size_t count) {     COLLECTION collection;     typename COLLECTION::value_type item;     for (size_t i = 0; i < count; i++) {       cin >> item;       collection.insert(item);     }     return collection;   }   template <typename COLLECTION>   void writeNums(const COLLECTION &collection) {     for (auto item : collection) {       cout << item <<    ;     }     cout << endl;   }   struct Datum {     Datum() {}     Datum(uint32_t _x, uint32_t _count) : x(_x), count(_count) {}     uint32_t x;     uint32_t count;   };   void pass(const vector<Datum> &data, uint32_t k, vector<Datum> &ranges) {     uint32_t idx_left = 0, idx_right = 0;     uint32_t left_value = 0;     while (idx_left < data.size() && idx_right < data.size()) {       while (idx_right < data.size()) {         if (idx_right + 1 < data.size() &&             data[idx_right + 1].x <= data[idx_left].x + k) {           idx_right++;         } else {           break;         }       }       ranges.push_back(           Datum(data[idx_left].x, data[idx_right].count - left_value));       left_value = data[idx_left].count;       idx_left++;     }   }   uint32_t calc(uint32_t n, uint32_t k) {     map<uint32_t, uint32_t> xs;     for (size_t i = 0; i < n; i++) {       uint32_t x;       cin >> x;       xs[x]++;     }     skipInputLine();     skipInputLine();     uint32_t cumulative = 0;     vector<Datum> data(xs.size());     uint32_t index = 0;     for (map<uint32_t, uint32_t>::const_iterator it = xs.begin(); it != xs.end();          it++) {       cumulative += it->second;       data[index++] = Datum(it->first, cumulative);     }     xs.clear();     uint32_t width = data[data.size() - 1].x - data[0].x + 1;     if (width <= 2 * (k + 1)) {       return n;     }     vector<Datum> ranges;     ranges.reserve(2 * data.size());     pass(data, k, ranges);     data.clear();     map<uint32_t, uint32_t> best_sums;     uint32_t bs = 0;     for (size_t i = 0; i < ranges.size(); i++) {       const Datum &range = ranges[i];       bs = max(bs, range.count);       best_sums[range.x] = bs;     }     uint32_t best_sum = 0;     for (size_t i = 1; i < ranges.size(); i++) {       const Datum &range = ranges[i];       uint32_t sum = range.count;       int32_t x = int32_t(range.x) - int32_t(k + 1);       if (x < 0) {         x = 0;       }       map<uint32_t, uint32_t>::const_iterator it = best_sums.upper_bound(x);       if (it != best_sums.cbegin()) {         it--;         best_sum = max(best_sum, sum + it->second);       }     }     return best_sum;   }   int main() {     init();     uint32_t t = readNum<uint32_t>();     for (uint32_t i = 0; i < t; i++) {       uint32_t n, k;       cin >> n >> k;       uint32_t res = calc(n, k);       cout << res <<   n ;     }     cout << flush;     return 0;   }   