#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  for (long long int x = 1; x <= t; x++) {
    long long int n, k, i, ans = 1;
    cin >> n >> k;
    vector<long long int> a(n), l(n), r(n);
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) {
      long long int x;
      cin >> x;
    }
    sort(a.begin(), a.end());
    long long int left = 0, right = 0;
    while (right < n) {
      if (a[right] - a[left] <= k) {
        l[right] = max(right - left + 1, (right ? l[right - 1] : 0));
        right++;
      } else
        left++;
    }
    right = left = n - 1;
    while (left >= 0) {
      if (a[right] - a[left] <= k) {
        r[left] = max(right - left + 1, ((left < n - 1) ? r[left + 1] : 0));
        left--;
      } else
        right--;
    }
    for (i = 0; i < n - 1; i++) ans = max(ans, l[i] + r[i + 1]);
    cout << ans << "\n";
  }
}
