#include <bits/stdc++.h>
using namespace std;
template <typename A>
using VE = vector<A>;
template <typename A>
using USET = unordered_set<A>;
template <typename A>
using HEAP = priority_queue<A>;
template <typename A, typename B>
using PA = pair<A, B>;
template <typename A, typename B>
using UMAP = unordered_map<A, B>;
template <typename A>
using RHEAP = priority_queue<A, vector<A>, greater<A> >;
template <typename A>
A MAX(const A &a) {
  return a;
}
template <typename A>
A MIN(const A &a) {
  return a;
}
template <typename A>
A MAX(const A *a, const A *b) {
  return *max_element(a, b);
}
template <typename A>
A MIN(const A *a, const A *b) {
  return *min_element(a, b);
}
template <typename A, typename... B>
A MAX(const A &a, const B &...b) {
  return max(a, MAX(b...));
}
template <typename A, typename... B>
A MIN(const A &a, const B &...b) {
  return min(a, MIN(b...));
}
template <typename A, typename B = typename std::iterator_traits<A>::value_type>
B MAX(A a, A b) {
  return *max_element(a, b);
}
template <typename A, typename B = typename std::iterator_traits<A>::value_type>
B MIN(A a, A b) {
  return *min_element(a, b);
}
mt19937 rng(
    (unsigned int)chrono::steady_clock::now().time_since_epoch().count());
const int N = 200010, M = 1010;
int T;
int n, k;
int x[N], y[N];
int f[N], g[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  cin >> T;
  while (T--) {
    cin >> n >> k;
    for (int i = 0; i <= n + 1; i++) f[i] = g[i] = 0;
    for (int i = 1; i <= n; i++) cin >> x[i];
    for (int i = 1; i <= n; i++) cin >> y[i];
    sort(x + 1, x + 1 + n);
    f[1] = 1;
    for (int i = 2; i <= n; i++) {
      int l = 1, r = i;
      while (l < r) {
        int mid = l + r >> 1;
        if (x[i] - x[mid] <= k)
          r = mid;
        else
          l = mid + 1;
      }
      f[i] = max(f[i - 1], i - l + 1);
    }
    g[n] = 1;
    for (int i = n - 1; i; i--) {
      int l = i, r = n;
      while (l < r) {
        int mid = l + r + 1 >> 1;
        if (x[mid] - x[i] <= k)
          l = mid;
        else
          r = mid - 1;
      }
      g[i] = max(g[i + 1], r - i + 1);
    }
    int res = 0;
    for (int i = 1; i <= n; i++) res = max(res, f[i - 1] + g[i]);
    cout << res << '\n';
  }
  return 0;
}
