#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < (n); i++) cin >> a[i];
  ;
  int b[n];
  for (int i = 0; i < (n); i++) cin >> b[i];
  ;
  sort(a, a + n);
  int count[n + 1];
  count[n] = 0;
  int cur[n + 1];
  cur[n] = 0;
  int ptr = n - 1;
  for (int i = (n)-1; i >= (0); i--) {
    while (a[ptr] > a[i] + k) ptr--;
    cur[i] = ptr - i + 1;
    count[i] = max(count[i + 1], ptr - i + 1);
  }
  ptr = 0;
  int answer = 0;
  for (int i = (0); i < (n); i++) {
    while (ptr < n && a[ptr] <= a[i] + k) ptr++;
    answer = max(answer, cur[i] + count[ptr]);
  }
  cout << answer << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
