#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int dp[maxn][2];
int a[maxn], b[maxn], sum[maxn];
int main() {
  int t, n, k;
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &b[i]);
    }
    sort(a, a + n);
    int pre = 0, cnt = -1;
    for (int i = 0; i < n; i++) {
      if (a[i] == pre)
        sum[cnt]++;
      else {
        pre = a[i];
        b[++cnt] = a[i];
        sum[cnt]++;
      }
    }
    cnt++;
    for (int i = 1; i < cnt; i++) {
      sum[i] += sum[i - 1];
    }
    for (int i = 0; i < cnt; i++) {
      int l = b[i] - k;
      int idx = lower_bound(b, b + cnt, l) - b;
      if (idx == 0) {
        dp[i][0] = sum[i];
        dp[i][1] = sum[i];
      } else {
        idx--;
        dp[i][0] = max(sum[i] - sum[idx], dp[i - 1][0]);
        dp[i][1] = sum[i] - sum[idx] + dp[idx][0];
      }
    }
    int ans = 0;
    for (int i = 0; i < cnt; i++) {
      ans = max(ans, dp[i][1]);
      dp[i][0] = 0, dp[i][1] = 0;
      sum[i] = 0;
    }
    printf("%d\n", ans);
  }
}
