#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
std::string vec_to_string(const std::vector<T>& v, std::string delim = " ") {
  if (v.empty()) {
    return "";
  }
  std::stringstream ss;
  auto it = v.cbegin();
  ss << *it++;
  for (; it != v.cend(); it++) {
    ss << delim << *it;
  }
  return ss.str();
}
void work() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> pxs(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &pxs[i]);
  }
  vector<ll> pys(n);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &pys[i]);
  }
  vector<ll> xxs(pxs.begin(), pxs.end());
  sort(xxs.begin(), xxs.end());
  vector<ll> xs{xxs.front()};
  vector<int> idxs{0};
  for (int i = 1; i < n; i++) {
    ll item = xxs[i];
    if (item != xs.back()) {
      xs.push_back(item);
      idxs.push_back(i);
    }
  }
  const int total = xxs.size();
  const int xs_size = xs.size();
  vector<int> x_counts_fwd(xs_size + 1);
  vector<int> x_counts_bck(xs_size + 1);
  for (int i = 0; i < xs_size; i++) {
    const int x_bgn = xs[i];
    int lo = i;
    int hi = xs_size;
    while (hi - lo > 1) {
      int mid = lo + (hi - lo) / 2;
      if (xs[mid] - x_bgn <= k) {
        lo = mid;
      } else {
        hi = mid;
      }
    }
    int xxs_next_idx = hi == xs_size ? total : idxs[hi];
    int k_count = xxs_next_idx - idxs[i];
    x_counts_fwd[i] = k_count;
    if (x_counts_bck[hi] == 0) {
      x_counts_bck[hi] = k_count;
    }
  }
  for (int i = xs_size; i > 0; i--) {
    if (x_counts_bck[i] != 0) {
      continue;
    }
    int hi = i - 1;
    const int x_last = xs[hi];
    int lo = -1;
    while (hi - lo > 1) {
      int mid = lo + (hi - lo) / 2;
      if (x_last - xs[mid] <= k) {
        hi = mid;
      } else {
        lo = mid;
      }
    }
    int xxs_bgn_idx = idxs[hi];
    int xxs_end_idx = i == xs_size ? total : idxs[i];
    int k_count = xxs_end_idx - xxs_bgn_idx;
    x_counts_bck[i] = k_count;
  }
  vector<int> max_left(xs_size + 1);
  vector<int> max_right(xs_size + 1);
  for (int i = 1; i <= xs_size; i++) {
    int max_count = x_counts_bck[i];
    if (max_left[i - 1] > max_count) {
      max_count = max_left[i - 1];
    }
    max_left[i] = max_count;
  }
  for (int i = xs_size - 1; i >= 0; i--) {
    int max_count = x_counts_fwd[i];
    if (max_right[i + 1] > max_count) {
      max_count = max_right[i + 1];
    }
    max_right[i] = max_count;
  }
  int max_answ = 0;
  for (int i = 0; i <= xs_size; i++) {
    int max_sum = max_left[i] + max_right[i];
    if (max_answ < max_sum) {
      max_answ = max_sum;
    }
  }
  cout << max_answ << "\n";
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    work();
  }
  return 0;
}
