#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  int flag = 1;
  char ch = getchar();
  while (48 > ch || ch > 57) {
    if (ch == 45) flag = -1;
    ch = getchar();
  }
  while (48 <= ch && ch <= 57) x = x * 10 + ch - 48, ch = getchar();
  x *= flag;
}
template <typename T>
inline void print(T x) {
  if (x < 0) putchar(45), x = -x;
  if (x > 9) print(x / 10);
  putchar(x % 10 + 48);
}
int x[2 * 100005];
int maxlen[2 * 100005];
pair<int, int> p[2 * 100005];
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n, k, y;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> x[i];
      maxlen[i] = 0;
    }
    for (int i = 1; i <= n; i++) cin >> y;
    sort(x + 1, x + 1 + n);
    int ans = 0;
    int num, sum;
    for (int i = 1; i <= n; i++) {
      num = upper_bound(x + 1, x + 1 + n, x[i] + k) - x;
      num -= i;
      ans = max(ans, num);
      p[i] = {i, i + num - 1};
    }
    for (int i = 1; i <= n; i++) {
      maxlen[p[i].second] =
          max(maxlen[p[i].second], p[i].second - p[i].first + 1);
    }
    for (int i = 1; i <= n; i++) {
      maxlen[i] = max(maxlen[i], maxlen[i - 1]);
    }
    for (int i = 1; i <= n; i++) {
      ans = max(ans, maxlen[p[i].first - 1] + p[i].second - p[i].first + 1);
    }
    cout << ans << '\n';
  }
  return 0;
}
