#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N], mx1[N], mx2[N];
int n, k;
int main() {
  int T;
  for (scanf("%d", &T); T--;) {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1, x; i <= n; i++) scanf("%d", &x);
    sort(a + 1, a + n + 1);
    memset(mx1, 0, sizeof(mx1));
    memset(mx2, 0, sizeof(mx2));
    for (int i = 1, j = 1; i <= n; i++) {
      for (; j <= n && a[i] + k >= a[j]; j++)
        ;
      mx2[i] = j - i;
    }
    for (int i = n, j = n; i; i--) {
      for (; j && a[i] - k <= a[j]; j--)
        ;
      mx1[i] = i - j;
    }
    for (int i = 1; i <= n; i++) mx1[i] = max(mx1[i - 1], mx1[i]);
    for (int i = n; i; i--) mx2[i] = max(mx2[i + 1], mx2[i]);
    int ans = 0;
    for (int i = 0; i <= n; i++) ans = max(ans, min(mx1[i] + mx2[i + 1], n));
    printf("%d\n", ans);
  }
}
