#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n), y(n);
    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> d1(n + 1), d2(n + 1);
    for (int i = 0, j = 0; i < n; i++) {
      while (x[i] > x[j] + k) j++;
      d1[i + 1] = i - j + 1;
    }
    for (int i = n - 1, j = n - 1; i >= 0; i--) {
      while (x[i] < x[j] - k) j--;
      d2[i] = j - i + 1;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) d1[i + 1] = max(d1[i + 1], d1[i]);
    for (int i = 0; i < n + 1; i++) ans = max(ans, d1[i] + d2[i]);
    cout << ans << "\n";
  }
  return 0;
}
