#include <bits/stdc++.h>
using namespace std;
int n, k, ans, y, t;
vector<int> a, v, h, sf;
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> k;
    sf.clear();
    sf.resize(n + 2, 0);
    a.resize(n + 1, 0);
    v.resize(n + 1, 0);
    h.resize(n + 1, 0);
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) cin >> y;
    sort(a.begin() + 1, a.begin() + n + 1);
    for (int i = 1; i <= n; ++i) {
      int l = lower_bound(a.begin() + 1, a.begin() + n + 1, a[i]) - a.begin(),
          hi = upper_bound(a.begin() + 1, a.begin() + n + 1, a[i] + k) -
               a.begin();
      v[i] = hi - l, h[i] = hi;
    }
    for (int i = n; i >= 1; --i) sf[i] = max(sf[i + 1], v[i]);
    ans = 0;
    for (int i = 1; i <= n; ++i) {
      int l = v[i], r = sf[h[i]];
      ans = max(ans, l + r);
    }
    cout << ans << '\n';
  }
  return 0;
}
