#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5, M = 205, INF = 1e9, MOD2 = 1e9 + 7;
int n, k, y, x[N], mem[N][3], freq[N];
int cur = 0, last = -1;
vector<int> vec;
int solve(int idx, int rem) {
  if (!rem) return 0;
  if (idx > cur) return 0;
  int& ret = mem[idx][rem];
  if (~ret) return ret;
  ret = 0;
  ret = solve(idx + 1, rem);
  int last =
      upper_bound(vec.begin(), vec.end(), vec[idx] + k) - vec.begin() - 1;
  int c2 = freq[last];
  if (idx) c2 -= freq[idx - 1];
  ret = max(ret, solve(last + 1, rem - 1) + c2);
  return ret;
}
void runtestcase() {
  cin >> n >> k;
  vec.clear();
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
    vec.push_back(x[i]);
    mem[i][2] = mem[i][1] = -1;
    freq[i] = 0;
  }
  freq[n] = 0;
  sort(vec.begin(), vec.end());
  cur = -1, last = -1;
  for (int i = 0; i < n; ++i) {
    if (last != vec[i]) last = vec[i], ++cur;
    freq[cur]++;
  }
  for (int i = 1; i <= cur; ++i) freq[i] += freq[i - 1];
  for (int i = 0; i < n; ++i) cin >> y;
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << solve(0, 2);
}
int main() {
  cout << fixed << setprecision(12), ios::sync_with_stdio(false),
      cin.tie(nullptr), cout.tie(nullptr);
  int t = 1, tt = 1;
  cin >> t;
  while (t) {
    runtestcase();
    cout << '\n';
    --t, ++tt;
  }
}
