#include <bits/stdc++.h>
using namespace std;
const long long M = 2e5 + 10;
void Solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  int e;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) cin >> e;
  sort(a.begin(), a.end());
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    int id = lower_bound(a.begin(), a.end(), a[i] - k) - a.begin();
    dp[i] = id;
  }
  vector<int> mx(n);
  mx[0] = 1;
  for (int i = 1; i < n; i++) mx[i] = max(mx[i - 1], i - dp[i] + 1);
  int x = 0;
  for (int i = 0; i < n; i++) {
    int t = i - dp[i] + 1;
    if (dp[i]) {
      t += mx[dp[i] - 1];
    }
    x = max(x, t);
  }
  cout << x << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) Solve();
  return 0;
}
