#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int N = 200005;
int a[N];
int suf[N];
int main() {
  int _;
  scanf("%d", &_);
  while (_--) {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%*d");
    sort(a, a + n);
    suf[n] = 0;
    for (int i = n - 1; i >= 0; i--) {
      int t = upper_bound(a, a + n, a[i] + m) - a - i;
      suf[i] = max(suf[i + 1], t);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      int k = upper_bound(a, a + n, a[i] + m) - a;
      ans = max(ans, k - i + suf[k]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
