#include <bits/stdc++.h>
using namespace std;
long long sqr(long long x) { return x * x; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    map<long long, long long> m;
    long long arr[n], brr[n];
    for (long long i = 0; i < n; i++) cin >> arr[i];
    for (long long i = 0; i < n; i++) cin >> brr[i];
    for (long long i = 0; i < n; i++) m[arr[i]]++;
    vector<long long> v;
    for (auto i : m) v.push_back(i.first);
    sort(v.begin(), v.end());
    long long cnt[n + 1], maxi[n + 1], ans = 0;
    for (long long i = 0; i < v.size(); i++) {
      long long x = lower_bound(v.begin(), v.end(), v[i] - k) - v.begin();
      if (i == 0) {
        cnt[i] = m[v[i]];
        maxi[i] = cnt[i];
        ans = maxi[i];
      } else if (x == 0 && v[x] + k >= v[i]) {
        cnt[i] = cnt[i - 1] + m[v[i]];
        maxi[i] = cnt[i];
        ans = max(ans, maxi[i]);
      } else if (x != 0 && v[x] + k >= v[i]) {
        cnt[i] = (cnt[i - 1] + m[v[i]]);
        maxi[i] = max(maxi[i - 1], cnt[i] - cnt[x - 1]);
        ans = max(ans, cnt[i] - cnt[x - 1] + maxi[x - 1]);
      } else {
        cnt[i] = cnt[i - 1] + m[v[i]];
        maxi[i] = max(maxi[i - 1], cnt[i] - cnt[x]);
        ans = max(ans, cnt[i] - cnt[x] + maxi[x]);
      }
    }
    cout << ans << endl;
  }
}
