#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n);
  for (int i = 0; i < n; i++) cin >> x[i];
  for (int i = 0, y; i < n; i++) cin >> y;
  sort(x.begin(), x.end());
  vector<int> cnt_left(n);
  int left = 0, i = 0;
  while (i < n) {
    if (x[i] - k <= x[left]) {
      if (i == 0)
        cnt_left[i] = 1;
      else
        cnt_left[i] = cnt_left[i - 1] + 1;
      i++;
    } else {
      while (left < i && x[i] - k > x[left]) left++;
      cnt_left[i] = i - left + 1;
      i++;
    }
  }
  vector<int> cnt_right(n);
  int right = n - 1, j = n - 1;
  while (j >= 0) {
    if (x[j] + k >= x[right]) {
      if (j == n - 1)
        cnt_right[j] = 1;
      else
        cnt_right[j] = cnt_right[j + 1] + 1;
      j--;
    } else {
      while (right > j && x[j] + k < x[right]) right--;
      cnt_right[j] = right - j + 1;
      j--;
    }
  }
  int ans = 0;
  vector<pair<int, int>> mx_left(n, {-1, -1});
  for (int i = 0; i < n; i++) {
    if (i == 0)
      mx_left[i] = {cnt_left[i], i};
    else {
      if (mx_left[i - 1].first < cnt_left[i])
        mx_left[i] = {cnt_left[i], i};
      else
        mx_left[i] = mx_left[i - 1];
    }
  }
  vector<pair<int, int>> mx_right(n, {-1, -1});
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      mx_right[i] = {cnt_right[i], i};
    else {
      if (mx_right[i + 1].first < cnt_right[i])
        mx_right[i] = {cnt_right[i], i};
      else
        mx_right[i] = mx_right[i + 1];
    }
  }
  for (int i = 0; i < n; i++) {
    if (mx_left[i].second != mx_right[i].second) {
      ans = max(ans, mx_left[i].first + mx_right[i].first);
    } else {
      ans = max(ans, mx_left[i].first + mx_right[i].first - 1);
    }
  }
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
