#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    int n, k;
    cin >> n >> k;
    vector<int> x(n), y(n);
    for (int &i : x) {
      cin >> i;
    }
    for (int &i : y) {
      cin >> i;
    }
    sort(x.begin(), x.end());
    int ans = 0;
    for (int i = 0, j = 0, dist2 = 0; i < x.size(); ++i) {
      while (j < x.size() && x[j] - x[i] <= k) {
        ++j;
      }
      int dist1 = j - i;
      if (i) {
        auto it = lower_bound(x.begin(), x.begin() + i, x[i - 1] - k);
        dist2 = max(dist2, (int)((x.begin() + i) - it));
      }
      ans = max(ans, dist1 + dist2);
    }
    for (int i = x.size() - 1, j = i, dist2 = 0; i >= 0; --i) {
      while (j >= 0 && x[i] - x[j] <= k) {
        --j;
      }
      int dist1 = i - j;
      if (i + 1 < x.size()) {
        auto it = upper_bound(x.begin() + i + 1, x.end(), x[i + 1] + k);
        dist2 = max(dist2, (int)(it - (x.begin() + i + 1)));
      }
      ans = max(ans, dist1 + dist2);
    }
    cout << ans << '\n';
  }
  return 0;
}
