#include <bits/stdc++.h>
using namespace std;
struct abc {
  long long val, r;
};
void solve() {
  const int Max = 200006;
  long long n, k, nr, a[Max];
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> nr;
  sort(a + 1, a + n + 1);
  abc best[Max];
  for (int i = 1; i <= n; i++) {
    long long l = i, r = n, left = i, mx = 1;
    while (l <= r) {
      long long mid = (l + r) / 2;
      if (a[mid] - a[i] <= k) {
        long long pcte = mid - i + 1;
        if (pcte > mx || (pcte == mx && mid < left)) {
          mx = pcte;
          left = mid;
        }
        l = mid + 1;
      } else
        r = mid - 1;
    }
    best[i].val = mx;
    best[i].r = left;
  }
  long long mx[Max];
  mx[n] = best[n].val;
  mx[n + 1] = 0;
  for (int i = n - 1; i >= 1; i--) mx[i] = max(mx[i + 1], best[i].val);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, best[i].val + mx[best[i].r + 1]);
  }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
