#include <bits/stdc++.h>
using namespace std;
int n;
int k;
int pre[200001];
int maxim[200001];
map<int, int> ct;
map<int, int> pos;
map<int, int> revpos;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    ct.clear();
    pos.clear();
    revpos.clear();
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      ct[x]++;
    }
    for (int i = 1; i <= n; i++) {
      int y;
      cin >> y;
    }
    int ind = 1;
    for (auto it : ct) {
      pos[it.first] = ind;
      revpos[ind] = it.first;
      pre[ind] = it.second;
      ind++;
    }
    int N = ind - 1;
    for (int i = 1; i <= N; i++) pre[i] += pre[i - 1];
    int bestAns = 0;
    for (int i = N; i >= 1; i--) {
      int rpos = revpos[i];
      maxim[i] = 0;
      if (i < N) maxim[i] = maxim[i + 1];
      auto it = pos.upper_bound(rpos + k);
      if (it == pos.end()) {
        bestAns =
            (bestAns > pre[N] - pre[i - 1] ? bestAns : pre[N] - pre[i - 1]);
        maxim[i] =
            (maxim[i] > pre[N] - pre[i - 1] ? maxim[i] : pre[N] - pre[i - 1]);
      } else {
        bestAns =
            (bestAns > pre[(*it).second - 1] - pre[i - 1] + maxim[(*it).second]
                 ? bestAns
                 : pre[(*it).second - 1] - pre[i - 1] + maxim[(*it).second]);
        maxim[i] = (maxim[i] > pre[(*it).second - 1] - pre[i - 1]
                        ? maxim[i]
                        : pre[(*it).second - 1] - pre[i - 1]);
      }
    }
    cout << bestAns << "\n";
  }
}
