#include <bits/stdc++.h>
using namespace std;
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};
int32_t main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, k, w;
    cin >> n >> k;
    vector<int> cx(n);
    for (auto& x : cx) cin >> x;
    for (int i = 0; i < n; i++) cin >> w;
    sort((cx).begin(), (cx).end());
    vector<int> f(n + 1), s(n + 1);
    for (int i = 0, j = 0; i < n; i++) {
      while (cx[i] - cx[j] > k) j++;
      f[i + 1] = max(f[i], i - j + 1);
    }
    for (int i = n - 1, j = n - 1; i >= 0; i--) {
      while (cx[j] - cx[i] > k) j--;
      s[i] = max(s[i + 1], j - i + 1);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) ans = max(ans, f[i] + s[i]);
    cout << ans << "\n";
  }
  return 0;
}
