#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<pair<long long, long long> > vec(n);
  for (long long i = 0; i < n; i++) cin >> vec[i].first;
  for (long long i = 0; i < n; i++) cin >> vec[i].second;
  sort(vec.begin(), vec.end());
  long long res[n];
  for (long long i = 0; i < n; i++) {
    long long cx = vec[i].first;
    long long mx = cx + k;
    long long s = i, e = n - 1, ans = i;
    while (s <= e) {
      long long m = (s + e) / 2;
      if (vec[m].first <= mx)
        ans = m, s = m + 1;
      else
        e = m - 1;
    }
    res[i] = ans - i + 1;
  }
  long long mx[n];
  for (long long i = 0; i < n; i++) mx[i] = res[i];
  for (long long i = n - 2; i >= 0; --i) mx[i] = max(mx[i], mx[i + 1]);
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long a = res[i];
    long long cx = vec[i].first;
    long long ls = cx + k;
    long long s = i, e = n - 1, a2 = n;
    while (s <= e) {
      long long m = (s + e) / 2;
      if (vec[m].first > ls)
        a2 = m, e = m - 1;
      else
        s = m + 1;
    }
    if (a2 < n) a += mx[a2];
    ans = max(ans, a);
  }
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  for (long long i = 0; i < t; i++) solve();
}
