#include <bits/stdc++.h>
using namespace std;
bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); }
long long C(long long n, long long k) {
  if (k > n) return 0;
  if (k * 2 > n) k = n - k;
  if (k == 0) return 1;
  long long result = n;
  for (long long i = 2; i <= k; ++i) {
    result *= (n - i + 1);
    result /= i;
  }
  return result;
}
long long power(long long x, unsigned long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
const long long Nmax = 1e5 + 2;
vector<long long> adj[Nmax];
bool v[Nmax];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    array<long long, 2> a[n];
    for (long long i = 0; i < n; ++i) cin >> a[i][0];
    for (long long i = 0; i < n; ++i) cin >> a[i][1];
    sort(a, a + n);
    vector<long long> vec;
    for (long long i = 0; i < n; ++i) vec.push_back(a[i][0]);
    long long d[n];
    for (long long i = 0; i < n; ++i) {
      auto it = upper_bound(vec.begin(), vec.end(), vec[i] + k);
      d[i] = it - vec.begin() - i;
    }
    long long maxx[n];
    maxx[n - 1] = d[n - 1];
    long long Max = maxx[n - 1];
    for (long long i = n - 2; i >= 0; --i) {
      Max = max(Max, d[i]);
      maxx[i] = Max;
    }
    long long ans = 0;
    for (long long i = 0; i < n; ++i) {
      long long c = 0;
      c += d[i];
      if (i + d[i] < n) c += maxx[i + d[i]];
      ans = max(ans, c);
    }
    cout << ans << "\n";
  }
  return 0;
}
