#include <bits/stdc++.h>
using namespace std;
int x[200007], y[200007], cnt[200007], mxcntback[200007];
int main() {
  int t, n, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++) scanf("%d", &x[i]);
    for (int i = 0; i < n; i++) scanf("%d", &y[i]);
    sort(x, x + n);
    for (int i = 0; i < n; i++) {
      int idx = upper_bound(x, x + n, x[i] + k) - x;
      cnt[i] = idx - i;
    }
    mxcntback[n - 1] = cnt[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      mxcntback[i] = max(mxcntback[i + 1], cnt[i]);
    }
    long long ans = -1;
    for (int i = 0; i < n; i++) {
      long long tot = cnt[i];
      if (i + cnt[i] < n) tot += mxcntback[i + cnt[i]];
      ans = max(ans, tot);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
