#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e17;
const long double pi = acos(-1.0);
const int maxn = 1e6 + 5;
int kase;
int n, m;
int a[maxn], b[maxn], c[maxn], x, y;
void run() {
  cin >> x >> y;
  for (int i = 1; i <= x; i++) {
    cin >> a[i];
  }
  int asdf;
  for (int i = 1; i <= x; i++) cin >> asdf;
  sort(a + 1, a + x + 1);
  for (int i = 1; i <= x; i++) {
    b[i] = (upper_bound(a + 1, a + x + 1, a[i] + y) - a) - i;
  }
  for (int i = x; i; i--) {
    c[i] = max(b[i], c[i + 1]);
  }
  int ans = 0;
  for (int i = 1; i <= x; i++) {
    ans = max(ans, b[i] + c[i + b[i]]);
  }
  cout << ans << '\n';
  for (int i = 1; i <= x; i++) b[i] = c[i] = 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> kase;
  while (kase--) run();
  return 0;
}
