#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
vector<int> primes(1, 2);
vector<vector<long double>> nk;
void sieve() {
  vector<int> m(N + 1, 0);
  for (int i = 4; i < N; i += 2) m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i) m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i]) primes.push_back(i);
}
void pascal() {
  nk = vector<vector<long double>>(N, vector<long double>(N, 0.0));
  for (int i = (0), ThxMK = (N); i < ThxMK; ++i) nk[i][0] = nk[i][i] = 1.0;
  for (int i = (1), ThxMK = (N); i < ThxMK; ++i)
    for (int j = (1), ThxMK = (i); j < ThxMK; ++j)
      nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}
long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }
long long lcm(long long a, long long b) {
  if (a < b) swap(a, b);
  long long c = gcd(a, b);
  a /= c;
  return a * b;
}
int popcount(long long x) { return __builtin_popcountll(x); }
long long poww(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}
long long powm(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}
bool isPrime(long long x) {
  if (x == 1) return 0;
  for (auto i : primes) {
    if (i * i > x) return 1;
    if (x % i == 0) return 0;
  }
  return 1;
}
long long SUM(long long a, long long b, long long mod = MOD) {
  return (a + b + mod) % mod;
}
long long PROD(long long a, long long b, long long mod = MOD) {
  a %= mod;
  b %= mod;
  return a * b % mod;
}
int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
vector<vector<int>> graph(int n, int m) {
  vector<vector<int>> v(n + 1);
  for (int i = (0), ThxMK = (m); i < ThxMK; ++i) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  return v;
}
void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = (0), ThxMK = (n); i < ThxMK; ++i) cin >> v[i];
  for (int i = (0), ThxMK = (n); i < ThxMK; ++i) {
    int a;
    cin >> a;
  }
  sort(begin(v), end(v));
  set<int> co;
  for (int i = (0), ThxMK = (n); i < ThxMK; ++i) co.insert(v[i]);
  vector<int> x;
  for (auto &a : co) x.push_back(a);
  n = int(x.size());
  vector<int> res(n, 0);
  for (int i = (0), ThxMK = (n); i < ThxMK; ++i) {
    int l = lower_bound(begin(v), end(v), x[i]) - v.begin();
    int r = upper_bound(v.begin() + l, v.end(), x[i] + k) - v.begin();
    res[i] = r - l;
  }
  int mx = 0;
  vector<int> suf;
  reverse(begin(res), end(res));
  suf.push_back(res[0]);
  for (int i = (1), ThxMK = (n); i < ThxMK; ++i)
    suf.push_back(max(suf.back(), res[i]));
  reverse(begin(suf), end(suf));
  reverse(begin(res), end(res));
  for (int i = (0), ThxMK = (n); i < ThxMK; ++i) {
    int p = x[i] + k, m2 = 0;
    int r = upper_bound(x.begin() + i, x.end(), p) - x.begin();
    if (r != int(x.size())) m2 = suf[r];
    mx = max(res[i] + m2, mx);
  }
  cout << mx << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
