#include <bits/stdc++.h>
using namespace std;
long long n, k, ans, cnt;
long long X[200000 + 9], Y[200000 + 9], preffixCount[200000 + 9],
    suffixCount[200000 + 9];
void helpMeBro() {
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> X[i];
  for (long long i = 0; i < n; i++) cin >> Y[i];
  sort(X, X + n);
  long long i, j;
  for (i = 0, j = 0, cnt = 0; i < n; i++) {
    cnt++;
    while (X[i] - X[j] > k) {
      j++;
      cnt--;
    }
    preffixCount[i] = cnt;
    if (i) preffixCount[i] = max(cnt, preffixCount[i - 1]);
  }
  for (i = n - 1, j = n - 1, cnt = 0; i >= 0; i--) {
    cnt++;
    while (X[j] - X[i] > k) {
      j--;
      cnt--;
    }
    suffixCount[i] = cnt;
    if (i != n - 1) suffixCount[i] = max(cnt, suffixCount[i + 1]);
  }
  ans = suffixCount[0];
  for (long long i = 1; i < n; i++) {
    ans = max(ans, suffixCount[i] + preffixCount[i - 1]);
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) helpMeBro();
  return 0;
}
