#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n, 0), y(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> y[i];
  }
  sort(x.begin(), x.end());
  vector<int> l(n, 0), r(n, 0);
  int lp = 0, rp = 0;
  for (int i = 0; i < n; i++) {
    while (lp < n - 1 && x[i] - x[lp] > k) {
      lp++;
    }
    while (rp < n - 1 && x[rp + 1] - x[i] <= k) {
      rp++;
    }
    l[i] = i - lp + 1;
    if (i > 0) l[i] = max(l[i], l[i - 1]);
    r[i] = rp - i + 1;
  }
  for (int i = n - 2; i >= 0; i--) {
    r[i] = max(r[i], r[i + 1]);
  }
  if (n == 1) {
    cout << 1 << endl;
  } else {
    int maxV = 0;
    for (int i = 0; i < n - 1; i++) {
      maxV = max(maxV, l[i] + r[i + 1]);
    }
    cout << maxV << endl;
  }
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return 0;
}
