#include <bits/stdc++.h>
using namespace std;
void fileIO() {}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fileIO();
  long long T = 1;
  cin >> T;
  while (T--) {
    long long n, k, x, y, i, j;
    cin >> n >> k;
    map<long long, long long> mp;
    for (i = 0; i < n; i++) {
      cin >> x;
      mp[x]++;
    }
    for (i = 0; i < n; i++) {
      cin >> x;
    }
    n = mp.size();
    vector<long long> values(n + 1), prefix(n + 2), coord(n + 2),
        bestfor(n + 2);
    long long idx = 1;
    values[0] = 0;
    for (auto x : mp) {
      coord[idx] = x.first;
      values[idx++] = x.second;
    }
    coord[idx] = 1e12;
    coord[0] = -1e12;
    prefix[0] = 0;
    for (i = 1; i <= n; i++) {
      prefix[i] = prefix[i - 1] + values[i];
    }
    prefix[n + 1] = prefix[n];
    long long mx = 0;
    j = 0;
    bestfor[0] = 0;
    for (i = 1; i <= n; i++) {
      while (coord[i] - coord[j] + 1 > k + 1) {
        j++;
      }
      mx = max(mx, prefix[i] - prefix[j - 1]);
      bestfor[i] = mx;
    }
    j = n + 1;
    mx = 0;
    long long ans = 0;
    for (i = n; i >= 1; i--) {
      while (coord[j] - coord[i] + 1 > k + 1) {
        j--;
      }
      mx = max(mx, prefix[j] - prefix[i - 1]);
      ans = max(ans, mx + bestfor[i - 1]);
    }
    cout << ans << "\n";
  }
  return 0;
}
