#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897;
const long long mod = 1000000007LL;
const long long INF = 1e18;
void f() {
  long long n, k;
  cin >> n >> k;
  vector<long long> x(n);
  for (long long &i : x) cin >> i;
  vector<long long> y(n);
  for (long long &i : y) cin >> i;
  if (n == 1) {
    cout << 1 << "\n";
    return;
  }
  sort((x).begin(), (x).end());
  vector<long long> d(n, 1);
  for (long long i = 0; i < n; i++) {
    auto it = upper_bound((x).begin(), (x).end(), x[i] + k);
    d[i] = it - (x.begin() + i);
  };
  ;
  vector<long long> pr(n, -INF), su(n, -INF);
  pr[0] = d[0];
  su[n - 1] = d[n - 1];
  for (long long i = 1; i < n; i++) {
    pr[i] = max(pr[i - 1], d[i - 1]);
  }
  for (long long i = n - 2; i > 0; i--) {
    su[i] = max(su[i + 1], d[i]);
  }
  long long jv = pr.back();
  for (long long i = 0; i < n; i++) {
    if (i + d[i] < n) jv = max(jv, d[i] + su[i + d[i]]);
  }
  cout << jv << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) f();
  return 0;
}
