#include <bits/stdc++.h>
using namespace std;
const long long N = 200200;
long long n, k;
map<long long, long long> mp;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    mp.clear();
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> x;
      mp[x]++;
    }
    for (long long i = 1; i <= n; i++) {
      long long x;
      cin >> x;
    }
    vector<pair<long long, long long>> v;
    for (auto i : mp) {
      v.push_back({i.first, i.second});
    }
    n = v.size();
    vector<long long> cnt(n);
    long long r = 0;
    long long tot = 0;
    multiset<long long> st;
    for (long long i = 0; i < n; i++) {
      while (r < n && v[r].first - v[i].first <= k) {
        tot += v[r].second;
        r++;
      }
      cnt[i] = tot;
      st.insert(cnt[i]);
      tot -= v[i].second;
    }
    long long ans = 0;
    r = 0;
    for (long long i = 0; i < n; i++) {
      while (r < n && v[r].first - v[i].first <= k) {
        st.erase(st.find(cnt[r]));
        r++;
      }
      if ((long long)st.size()) {
        ans = max(ans, cnt[i] + *st.rbegin());
      } else {
        ans = max(ans, cnt[i]);
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
