#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int a[N + 10], pre[N + 10], suf[N + 10];
int max1[N + 10], max2[N + 10];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    memset(pre, 0, sizeof(pre));
    memset(suf, 0, sizeof(suf));
    memset(max1, 0, sizeof(max1));
    memset(max2, 0, sizeof(max2));
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      int cnm;
      scanf("%d", &cnm);
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
      int l = 1, r = i, ans = 0;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[mid] >= a[i] - k) {
          ans = mid;
          r = mid - 1;
        } else
          l = mid + 1;
      }
      pre[i] = i - ans + 1;
      l = i;
      r = n;
      ans = 0;
      while (l <= r) {
        int mid = (l + r) / 2;
        if (a[mid] <= a[i] + k) {
          ans = mid;
          l = mid + 1;
        } else
          r = mid - 1;
      }
      suf[i] = ans - i + 1;
    }
    for (int i = 1; i <= n; i++) {
      max1[i] = max(max1[i - 1], pre[i]);
      max2[n - i + 1] = max(max2[n - i + 2], suf[n - i + 1]);
    }
    int ans = 0;
    for (int i = 1; i <= n + 1; i++) ans = max(ans, max1[i - 1] + max2[i]);
    printf("%d\n", ans);
  }
  return 0;
}
