#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
ll dp[210][210][210];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N;
  cin >> N;
  map<ll, ll> mp;
  vector<pii> v;
  while (N--) {
    ll n, K;
    cin >> n >> K;
    mp.clear();
    v.clear();
    ll t;
    for (ll i = 0; i <= n - 1; i++) {
      cin >> t;
      mp[t]++;
    }
    for (ll i = 0; i <= n - 1; i++) cin >> t;
    for (auto &i : mp) v.push_back(i);
    n = ((ll)(v.size()));
    vector<ll> start(n + 1, -(ll)(1e17)), end(n + 1, -(ll)(1e17));
    ll l = 0, now = 0;
    for (ll r = 0; r <= n - 1; r++) {
      now += v[r].second;
      while (l < n && v[r].first - v[l].first > K) {
        now -= v[l].second;
        l++;
      }
      assert(l <= r);
      start[l] = max(start[l], now);
      end[r + 1] = max(end[r], now);
    }
    ll ans = 0;
    now = 0;
    for (ll i = 0; i <= n; i++) {
      now = max(now, end[i]);
      ans = max(ans, now + start[i]);
    }
    cout << ans << "\n";
  }
  return 0;
}
