#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n = 0, m = 0, k = 0, sum = 0;
  string s;
  cin >> n >> k;
  vector<long long> a(n + 2, 0);
  vector<long long> l(n + 2, 0);
  vector<long long> r(n + 2, 0);
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  for (long long i = 1; i <= n; ++i) cin >> sum;
  sort(a.begin() + 1, a.end() - 1);
  long long q = 1, e = 1;
  while (q <= n) {
    while (e <= n && a[q] + k >= a[e]) e++;
    e--;
    e = max(q, e);
    long long len = e - q + 1;
    r[q] = len;
    q++;
    e = max(q, e);
  }
  q = n, e = n;
  while (e > 0) {
    while (q > 0 && a[e] - k <= a[q]) q--;
    q++;
    q = min(q, e);
    long long len = e - q + 1;
    l[e] = len;
    e--;
    q = min(q, e);
  }
  vector<long long> pref(n + 2, 0);
  vector<long long> suf(n + 2, 0);
  for (long long i = 1; i <= n; ++i) pref[i] = max(pref[i - 1], l[i]);
  for (long long i = n; i > 0; --i) suf[i] = max(suf[i + 1], r[i]);
  long long ans = -1e9;
  for (long long i = 0; i <= n; ++i) ans = max(ans, pref[i] + suf[i + 1]);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  long long qwe = 1;
  cin >> qwe;
  while (qwe--) {
    solve();
    cout << "\n";
  }
}
