#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  for (; t-- > 0;) {
    long long n, k;
    cin >> n >> k;
    vector<long long> x(n);
    for (auto& xi : x) {
      cin >> xi;
    }
    vector<long long> y(n);
    for (auto& yi : y) {
      cin >> yi;
    }
    sort(x.begin(), x.end());
    long long lhs = 0;
    long long rhs = 0;
    vector<long long> s(n);
    for (; lhs < n; ++lhs) {
      while (rhs < n && x[lhs] + k >= x[rhs]) {
        rhs++;
      }
      s[lhs] = rhs - lhs;
    }
    vector<long long> ms(n + 1);
    ms[n] = 0;
    for (long long i = n - 1; i >= 0; --i) {
      ms[i] = max(ms[i + 1], s[i]);
    }
    long long rv = 0;
    for (long long i = 0; i < n; ++i) {
      long long j = s[i] + i;
      rv = max(rv, s[i] + ms[j]);
    }
    cout << rv << endl;
  }
  return 0;
}
