#include <bits/stdc++.h>
using namespace std;
int t, n, k;
int x[200005];
int y[200005];
int count1[200005];
int max1[200005];
int main() {
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n >> k;
    for (int j = 1; j <= n; j++) {
      cin >> x[j];
    }
    for (int j = 1; j <= n; j++) {
      cin >> y[i];
    }
    sort(x + 1, x + n + 1);
    for (int j = 1; j <= n; j++) {
      count1[j] = upper_bound(x + 1, x + n + 1, x[j] + k) - (x + j);
    }
    for (int j = 1; j <= n; j++) {
      max1[j] = max(max1[j - 1], count1[j]);
    }
    int max2 = 0;
    for (int j = 1; j <= n; j++) {
      int temp = upper_bound(x + 1, x + n + 1, x[j] - k - 1) - (x + 1);
      max2 = max(max2, max1[temp] + count1[j]);
    }
    cout << max2 << endl;
  }
}
