#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, l;
  cin >> n >> l;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
  }
  int tor[n];
  int j = 0;
  for (int i = 0; i < n; i++) {
    while (arr[j] <= arr[i] + l && j < n) j++;
    tor[i] = (j - i);
  }
  int tol[n];
  j = n - 1;
  for (int i = n - 1; i >= 0; i--) {
    while (arr[i] <= arr[j] + l && j >= 0) j--;
    tol[i] = (i - j);
  }
  int maxi = 0;
  int maxitol = 0;
  for (int i = 0; i < n; i++) {
    if (i) maxitol = max(maxitol, tol[i - 1]);
    maxi = max(tor[i] + maxitol, maxi);
  }
  cout << maxi << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
