#include <bits/stdc++.h>
using namespace std;
long long MAXN = 2e5 + 5;
long long sum(long long n) {
  long long ans = 0;
  while (n > 0) {
    ans += n % 10;
    n = n / 10;
  }
  return ans;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, tmp;
    cin >> n >> k;
    vector<long long> v;
    for (long long i = 0; i < n; i++) {
      cin >> tmp;
      v.push_back(tmp);
    }
    for (long long i = 0; i < n; i++) {
      cin >> tmp;
    }
    sort(v.begin(), v.end());
    map<long long, long long> m;
    for (long long i = 0; i < n; i++) {
      m[v[i]] = max(m[v[i]],
                    upper_bound(v.begin(), v.end(), v[i] + k) - v.begin() - i);
    }
    map<long long, long long> rm;
    rm[v[n - 1]] = m[v[n - 1]];
    for (long long i = n - 2; i > -1; i--) {
      rm[v[i]] = max(rm[v[i + 1]], m[v[i]]);
    }
    long long ans = 0;
    map<long long, long long>::iterator it;
    for (it = m.begin(); it != m.end(); it++) {
      long long up = *upper_bound(v.begin(), v.end(), (it->first) + k);
      ans = max(ans, (it->second) + (rm[up]));
    }
    cout << min(ans, n) << endl;
  }
  return 0;
}
