#include <bits/stdc++.h>
using namespace std;
const int maxn = 200007;
int A[maxn];
int TREE[maxn << 2];
int T;
int N, K;
int d[maxn], sum[maxn], id[maxn], lt[maxn];
int Max[maxn];
int main() {
  scanf("%d", &T);
  while (T--) {
    memset(Max, 0, sizeof(Max));
    memset(sum, 0, sizeof(sum));
    scanf("%d%d", &N, &K);
    for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
    for (int i = 1; i <= N; i++) scanf("%d", &d[i]), d[i] = A[i];
    sort(d + 1, d + 1 + N);
    int tot = unique(d + 1, d + 1 + N) - d - 1;
    for (int i = 1; i <= N; i++) {
      id[i] = lower_bound(d + 1, d + 1 + tot, A[i]) - d;
      lt[id[i]] = lower_bound(d + 1, d + 1 + tot, A[i] - K) - d;
      sum[id[i]]++;
    }
    for (int i = 1; i <= tot; i++) sum[i] += sum[i - 1];
    int ans = 0;
    for (int i = 1; i <= tot; i++) {
      ans = max(ans, sum[i] - sum[lt[i] - 1] + Max[lt[i] - 1]);
      Max[i] = max(Max[i - 1], sum[i] - sum[lt[i] - 1]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
