#include <bits/stdc++.h>
using namespace std;
int x[200005];
int l[200005];
int r[200005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    int t;
    for (int i = 0; i < n; i++) {
      cin >> t;
    }
    sort(x, x + n);
    int j = 0;
    for (int i = 0; i < n; i++) {
      while (x[i] - x[j] > k) j++;
      l[i] = i - j + 1;
      if (i > 0) l[i] = max(l[i], l[i - 1]);
    }
    j = n - 1;
    for (int i = n - 1; i >= 0; i--) {
      while (x[j] - x[i] > k) j--;
      r[i] = j - i + 1;
      if (i < n - 1) r[i] = max(r[i], r[i + 1]);
    }
    int ans = 1;
    for (int i = 0; i < n - 1; i++) {
      ans = max(ans, l[i] + r[i + 1]);
    }
    cout << ans << endl;
  }
  return 0;
}
