#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    long long int a[n];
    for (long long int i = 0; i < n; i++) cin >> a[i];
    for (long long int i = 0; i < n; i++) {
      long long int x;
      cin >> x;
    }
    if (n == 1) {
      cout << 1 << endl;
      continue;
    }
    sort(a, a + n);
    long long int left[n];
    left[0] = 1;
    long long int j = 0;
    for (long long int i = 1; i < n; i++) {
      while (a[i] - a[j] > k) j++;
      left[i] = max(i - j + 1, left[i - 1]);
    }
    long long int right[n];
    right[n - 1] = 1;
    j = n - 1;
    for (long long int i = n - 2; i >= 0; i--) {
      while (a[j] - a[i] > k) j--;
      right[i] = max(j - i + 1, right[i + 1]);
    }
    long long int ans = 0;
    for (long long int i = 1; i < n; i++) {
      ans = max(ans, left[i - 1] + right[i]);
    }
    cout << ans << endl;
  }
}
