#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    vector<long long> x(n);
    vector<long long> 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<long long> max_val(n + 1, 0);
    for (int i = n - 1; i >= 0; i--) {
      long long greater =
          int(upper_bound(x.begin() + i, x.end(), x[i] + k) - x.begin()) - i;
      max_val[i] = max(greater, max_val[i + 1]);
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int new_idx =
          int(upper_bound(x.begin() + i, x.end(), x[i] + k) - x.begin());
      long long add = (new_idx - i) + max_val[new_idx];
      ans = max(ans, add);
    }
    cout << ans << endl;
  }
}
