#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long int var;
  cin >> var;
  while (var--) {
    long long int n, k;
    cin >> n >> k;
    long long int x[n], y[n];
    for (long long int i = 0; i < n; i++) cin >> x[i];
    for (long long int i = 0; i < n; i++) cin >> y[i];
    sort(x, x + n);
    long long int dp[n + 1], r = n - 1;
    dp[n] = 0;
    dp[n - 1] = 1;
    for (long long int i = n - 2; i >= 0; i--) {
      while (r != i) {
        if (x[r] - x[i] <= k)
          break;
        else
          r--;
      }
      dp[i] = r - i + 1;
    }
    long long int ma[n + 1];
    ma[n] = 0;
    long long int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
      long long int z = dp[i] + i;
      z = min(n, z);
      ma[i] = max(ma[i + 1], dp[i]);
      ans = max(ans, dp[i] + ma[z]);
    }
    cout << ans << '\n';
  }
}
