#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    cin >> n >> k;
    vector<int> x;
    for (int i = 0; i < n; ++i) {
      int nmb;
      cin >> nmb;
      x.push_back(nmb);
    }
    for (int i = 0; i < n; ++i) {
      int nmb;
      cin >> nmb;
    }
    sort(begin(x), end(x));
    vector<int> left(n, 1), right(n, 1);
    for (int i = 0; i < n; ++i) {
      left[i] = i - (lower_bound(begin(x), end(x), x[i] - k) - begin(x)) + 1;
      right[i] = upper_bound(begin(x), end(x), x[i] + k) - begin(x) - i;
    }
    for (int i = 1; i < n; ++i) left[i] = max(left[i], left[i - 1]);
    for (int i = n - 2; i >= 0; --i) right[i] = max(right[i], right[i + 1]);
    int ans = 1;
    for (int i = 0; i < n - 1; ++i) ans = max(ans, left[i] + right[i + 1]);
    cout << ans << endl;
  }
}
