#include <bits/stdc++.h>
using namespace std;
long long waste;
void solve() {
  long long n;
  long long k;
  cin >> n >> k;
  vector<long long> a(n);
  vector<long long> pref(n), suff(n);
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < n; i++) cin >> waste;
  sort(a.begin(), a.end());
  for (long long i = 0; i < n; i++) {
    auto cnt = lower_bound(a.begin(), a.end(), a[i] - k) - a.begin();
    pref[i] = i - cnt + 1;
    if (i) pref[i] = max(pref[i - 1], pref[i]);
  }
  for (long long i = n - 1; i >= 0; i--) {
    auto cnt = upper_bound(a.begin(), a.end(), a[i] + k) - a.begin();
    suff[i] = cnt - i;
    if (i != n - 1) suff[i] = max(suff[i + 1], suff[i]);
  }
  if (n == 1) {
    cout << "1"
         << "\n";
    return;
  }
  long long ans = 0;
  for (long long i = 0; i < n - 1; i++) ans = max(ans, pref[i] + suff[i + 1]);
  cout << ans << "\n";
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long test;
  cin >> test;
  while (test--) solve();
  return 0;
}
