#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t;
  cin >> t;
  for (int i = 0; i < (t); i++) {
    int n, k;
    cin >> n >> k;
    vi x(n), y(n);
    for (int j = 0; j < (n); j++) cin >> x[j];
    for (int j = 0; j < (n); j++) cin >> y[j];
    sort(x.begin(), x.end());
    vi l(n + 1), r(n + 1);
    for (int j = 0; j < (n); j++) {
      l[j + 1] = l[j];
      chmax(l[j + 1], (int)((x.begin() + j + 1) -
                            lower_bound(x.begin(), x.end(), x[j] - k)));
    }
    for (int j = (n)-1; j >= 0; j--) {
      r[j] = r[j + 1];
      chmax(r[j],
            (int)(upper_bound(x.begin(), x.end(), x[j] + k) - (x.begin() + j)));
    }
    int ans = 0;
    for (int j = 0; j < (n + 1); j++) chmax(ans, l[j] + r[j]);
    cout << ans << endl;
  }
}
