#include <bits/stdc++.h>
using namespace std;
long long solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> x(n), dp(n + 1, 0);
  for (long long i = 0; i < n; i++) cin >> x[i];
  long long t;
  for (long long i = 0; i < n; i++) cin >> t;
  sort(x.begin(), x.end());
  for (long long i = n - 1; i >= 0; i--) {
    long long t = upper_bound(x.begin(), x.end(), x[i] + k) - x.begin();
    dp[i] = max(dp[i + 1], t - i);
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long t = upper_bound(x.begin(), x.end(), x[i] + k) - x.begin();
    long long ex = 0;
    if (t < n and x[t] >= x[i] + k) ex = dp[t];
    ans = max(ans, t - i + ex);
  }
  cout << ans << "\n";
  return 0;
}
int32_t main() {
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
