#include <bits/stdc++.h>
using namespace std;
struct disp {
  int n;
  long long a[200039];
  int sz;
  long long acopy[200039];
  int lsh[200039];
  void init(int kn) {
    n = kn;
    memcpy(acopy, a, sizeof(a));
    sort(acopy + 1, acopy + 1 + n);
    sz = unique(acopy + 1, acopy + 1 + n) - (acopy + 1);
    for (int i = (1); i < n + 1; i++)
      lsh[i] = lower_bound(acopy + 1, acopy + 1 + sz, a[i]) - acopy;
  }
  int getsmall(long long x) {
    return lower_bound(acopy + 1, acopy + 1 + sz, x) - acopy;
  }
  long long getbig(int i) { return acopy[i]; }
} dsp;
long long n, k;
bool judge(int x, int left) { return dsp.getbig(x) - dsp.getbig(left) <= k; }
int Bsearch(int l, int r, int left) {
  while (l < r) {
    int mid = (l + r + 1) >> 1;
    if (judge(mid, left))
      l = mid;
    else
      r = mid - 1;
  }
  return l;
}
long long x[200039];
long long dottimes[200039];
long long dotsum[200039];
long long canget[200039];
long long maxcanget[200039];
long long rr[200039];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    scanf("%lld%lld", &n, &k);
    dsp.n = 0;
    for (int i = (1); i < n + 1; i++) {
      scanf("%lld", &dsp.a[++dsp.n]);
      x[i] = dsp.a[dsp.n];
    }
    long long tp;
    for (int i = (1); i < n + 1; i++) scanf("%lld", &tp);
    dsp.init(dsp.n);
    for (int i = (1); i < n + 1; i++) dottimes[dsp.getsmall(x[i])]++;
    for (int i = (1); i < dsp.sz + 1; i++)
      dotsum[i] = dotsum[i - 1] + dottimes[i];
    for (int i = (1); i < dsp.sz + 1; i++) {
      int pos = Bsearch(i, dsp.sz, i);
      rr[i] = pos;
      canget[i] = dotsum[pos] - dotsum[i - 1];
    }
    for (int i = (dsp.sz); i > 0; i--)
      maxcanget[i] = max(canget[i], maxcanget[i + 1]);
    long long ans = 0;
    for (int i = (1); i < dsp.sz + 1; i++)
      ans = max(ans, canget[i] + maxcanget[rr[i] + 1]);
    printf("%lld\n", ans);
    memset(dottimes, 0, (n + 1) * sizeof(long long));
    memset(dotsum, 0, (n + 1) * sizeof(long long));
    memset(canget, 0, (n + 1) * sizeof(long long));
    memset(maxcanget, 0, (n + 1) * sizeof(long long));
  }
  return 0;
}
