#include <bits/stdc++.h>
using namespace std;
int n, k, maxn[200005], a[200005], b[200005];
int main() {
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    sort(a + 1, a + 1 + n);
    for (int i = n; i >= 1; i--) {
      int kk = a[i] + k;
      int index = upper_bound(a + 1, a + 1 + n, kk) - a;
      maxn[i] = max(maxn[i + 1], index - i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int kk = a[i] + k;
      int index = upper_bound(a + 1, a + 1 + n, kk) - a;
      ans = max(ans, maxn[index] + index - i);
    }
    cout << ans << endl;
    for (int i = 0; i <= n + 1; i++) maxn[i] = 0;
  }
}
