#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 9;
int x[N], y[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> y[i];
    }
    sort(x, x + n);
    vector<int> rem;
    int maxi = 0, opt = 0, fans = 0;
    for (int i = 0; i < n; ++i) {
      int curr = upper_bound(x, x + n, x[i] + k) - x - i;
      if (i) {
        opt = (x + i - 1) - lower_bound(x, x + n, x[i - 1] - k);
        opt++;
      }
      maxi = max(maxi, opt);
      curr += maxi;
      fans = max(fans, curr);
    }
    cout << fans << "\n";
  }
  return 0;
}
