#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  solve();
}
const double EPS = 1e-6;
const int INF = ~(1 << 31);
const long long LINF = ~(1LL << 63);
const int MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
const long long MAXV = 1e9 + 1;
long long ans_last = INF;
long long ans_st = 0;
vector<int> d;
void add_one(int p) {
  d[p]++;
  while (d[p] > 9) {
    d[p] = 0;
    d[p + 1]++;
    p++;
  }
}
void solve() {
  long long t, n, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    vector<int> x(n), y(n);
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> y[i];
    }
    sort((x).begin(), (x).end());
    vector<int> d(n);
    int p = 0;
    for (int i = 0; i < n; ++i) {
      while (p < n && x[p] - x[i] <= k) {
        p++;
      }
      d[i] = p - i;
    }
    vector<int> f(n);
    f[n - 1] = d[n - 1];
    for (int i = n - 1 - 1; i >= 0; --i) {
      f[i] = max(d[i], f[i + 1]);
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int count = d[i];
      if (i + d[i] < n) {
        count += f[i + d[i]];
      }
      ans = max(ans, count);
    }
    cout << ans << "\n";
  }
}
