#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 2;
int ar[maxn], pre[maxn];
int t, n, k;
void init() { memset(pre, 0, sizeof(pre)); }
int main() {
  scanf("%d", &t);
  while (t--) {
    init();
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; ++i) {
      scanf("%d", &ar[i]);
    }
    for (int i = 0, a; i < n; ++i) {
      scanf("%d", &a);
    }
    sort(ar, ar + n);
    for (int i = n - 1; i >= 0; --i) {
      int now = ar[i], len = upper_bound(ar, ar + n, now + k) - ar;
      pre[i] = max(pre[i + 1], len - i);
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int now = ar[i], len = upper_bound(ar, ar + n, now + k) - ar;
      ans = max(ans, len - i + pre[len]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
