#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  int points[n];
  for (int i = 0; i < (n); i++) {
    cin >> points[i];
  }
  for (int i = 0; i < (n); i++) {
    int y;
    cin >> y;
  }
  sort(points, points + n);
  vector<pair<int, int> > collapse;
  collapse.push_back(make_pair(points[0] - k, 0));
  int curr = 0;
  for (int i = 1; i < n; i++) {
    if (points[i] != points[curr]) {
      collapse.push_back(make_pair(points[curr], i - curr));
      curr = i;
    }
  }
  collapse.push_back(make_pair(points[curr], n - curr));
  pair<int, int> pre[collapse.size()];
  int start = 0;
  pre[0] = make_pair(collapse[0].second, 0);
  for (int i = 1; i < collapse.size(); i++) {
    pre[i] = make_pair(pre[i - 1].first + collapse[i].second, start);
    while (collapse[i].first - collapse[start].first > k) {
      pre[i].first -= collapse[start].second;
      start++;
    }
    pre[i].second = start;
  }
  int dp[collapse.size()];
  dp[0] = pre[0].first;
  for (int i = 1; i < collapse.size(); i++) {
    dp[i] = max(pre[i].first, dp[i - 1]);
  }
  int res = 0;
  for (int i = 0; i < collapse.size(); i++) {
    res = max(res, pre[i].first +
                       ((pre[i].second - 1 >= 0) ? dp[pre[i].second - 1] : 0));
  }
  cout << res << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  ;
  int poo;
  cin >> poo;
  while (poo--) {
    solve();
  }
}
