#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll IINF = INT_MAX;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n;
    ll k;
    cin >> n >> k;
    int x[n], y[n];
    map<int, ll> cnt;
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> y[i];
      cnt[x[i]]++;
    }
    vector<int> ar;
    vll fro, bac;
    for (auto it : cnt) {
      ar.push_back(it.first);
      fro.push_back(it.second);
      bac.push_back(it.second);
    }
    for (int i = 1; i < (int)(ar).size(); i++) fro[i] += fro[i - 1];
    for (int i = (int)(ar).size() - 2; i >= 0; i--) bac[i] += bac[i + 1];
    vll pfr((int)(ar).size()), pba((int)(ar).size());
    for (int i = 0; i < (int)(ar).size(); i++) {
      int l = i, r = (int)(ar).size() - 1, m, best = -1;
      while (l <= r) {
        m = (l + r) / 2;
        if (ar[i] + k >= ar[m]) {
          best = m;
          l = m + 1;
        } else {
          r = m - 1;
        }
      }
      pfr[i] = fro[best];
      if (i > 0) pfr[i] -= fro[i - 1];
    }
    for (int i = 0; i < (int)(ar).size(); i++) {
      int l = 0, r = i, m, best = -1;
      while (l <= r) {
        m = (l + r) / 2;
        if (ar[i] - k <= ar[m]) {
          best = m;
          r = m - 1;
        } else {
          l = m + 1;
        }
      }
      pba[i] = bac[best];
      if (i < (int)(ar).size() - 1) pba[i] -= bac[i + 1];
    }
    for (int i = 1; i < (int)(ar).size(); i++) {
      pba[i] = max(pba[i], pba[i - 1]);
    }
    vll lol((int)(ar).size());
    for (int i = 0; i < (int)(ar).size(); i++) {
      lol[i] = pfr[i];
    }
    for (int i = (int)(ar).size() - 2; i >= 0; i--) {
      pfr[i] = max(pfr[i], pfr[i + 1]);
    }
    ll ans = 0;
    for (int i = 0; i < (int)(ar).size(); i++) {
      ll satu = lol[i], dua = lol[i];
      if (i > 0) {
        satu += pba[i - 1];
      }
      int l = i, r = (int)(ar).size() - 1, m, best = -1;
      while (l <= r) {
        m = (l + r) / 2;
        if (ar[i] + k >= ar[m]) {
          best = m;
          l = m + 1;
        } else {
          r = m - 1;
        }
      }
      best++;
      if (best < (int)(ar).size()) {
        dua += pfr[best];
      }
      satu = max(satu, dua);
      ans = max(ans, satu);
    }
    cout << ans << '\n';
  }
  return 0;
}
