#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long n, k;
long long a[N];
long long l[N], r[N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    long long x, y;
    for (long long i = 1; i <= n; i++) {
      cin >> x;
      a[i] = x;
    }
    for (long long i = 1; i <= n; i++) cin >> y;
    sort(a + 1, a + n + 1);
    long long j = 1;
    for (long long i = 1; i <= n; i++) {
      while (a[i] - a[j] > k) j++;
      l[i] = i - j + 1;
      if (i > 1) l[i] = max(l[i], l[i - 1]);
    }
    j = n;
    for (long long i = n; i >= 1; i--) {
      while (a[j] - a[i] > k) j--;
      r[i] = j - i + 1;
      if (i + 1 <= n) r[i] = max(r[i], r[i + 1]);
    }
    long long ans = 1;
    for (long long i = 1; i + 1 <= n; i++) {
      ans = max(ans, l[i] + r[i + 1]);
    }
    cout << ans << "\n";
  }
  return 0;
}
