#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k, y;
    scanf("%d %d", &n, &k);
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
      scanf("%d", &y);
    }
    sort(a.begin(), a.end());
    vector<int> seg(n), idx(n), max_seg(n);
    for (int i = 0; i < n; i++) {
      int id = upper_bound(a.begin(), a.end(), a[i] + k) - a.begin();
      seg[i] = (id - i);
      idx[i] = id;
    }
    max_seg[n - 1] = seg[n - 1];
    for (int i = n - 2; i >= 0; i--) {
      max_seg[i] = max(max_seg[i + 1], seg[i]);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans = max(ans, seg[i] + (idx[i] == n ? 0 : max_seg[idx[i]]));
    }
    printf("%d\n", ans);
  }
}
