#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool d = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') d = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return d ? x : -x;
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = 2e5 + 5;
int a[N], pre[N], suf[N];
int main() {
  int T = read();
  while (T--) {
    int n = read(), k = read();
    for (int i = (int)(1); i <= (int)(n); i++) a[i] = read();
    for (int i = (int)(1); i <= (int)(n); i++) read();
    sort(a + 1, a + n + 1);
    int pos = 1;
    for (int i = (int)(1); i <= (int)(n); i++) {
      while (a[i] - a[pos] > k) pos++;
      pre[i] = max(pre[i - 1], i - pos + 1);
    }
    pos = n;
    suf[n + 1] = 0;
    for (int i = (int)(n); i >= (int)(1); i--) {
      while (a[pos] - a[i] > k) pos--;
      suf[i] = max(suf[i + 1], pos - i + 1);
    }
    int ans = 0;
    for (int i = (int)(0); i <= (int)(n); i++)
      ans = max(ans, pre[i] + suf[i + 1]);
    cout << ans << endl;
  }
}
