#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    vector<int> y(n);
    for (int i = 0; i < n; i++) {
      cin >> y[i];
    }
    sort(x.begin(), x.end());
    vector<int> start;
    vector<int> len;
    for (int i = 0; i < n; i++) {
      start.push_back(x[i]);
      int pos = upper_bound(x.begin(), x.end(), x[i] + k) - x.begin();
      len.push_back(pos - i);
      while (i + 1 < n && x[i] == x[i + 1]) {
        i++;
      }
    }
    int sz = start.size();
    int K = 33 - __builtin_clz(sz);
    vector<vector<int>> st(sz, vector<int>(K));
    for (int i = 0; i < sz; i++) {
      st[i][0] = len[i];
    }
    for (int j = 1; (1 << j) < sz; j++) {
      for (int i = 0; i + (1 << j) - 1 < sz; i++) {
        st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
      }
    }
    auto query = [&](int l, int r) {
      int d = 31 - __builtin_clz(r - l + 1);
      return max(st[l][d], st[r - (1 << d) + 1][d]);
    };
    int ans = 0;
    for (int i = 0; i < sz; i++) {
      int new_start =
          int(lower_bound(start.begin(), start.end(), start[i] + k + 1) -
              start.begin());
      int cur = start[i] + k + 1 > start[sz - 1] ? 0 : query(new_start, sz - 1);
      ans = max(ans, cur + len[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
