#include <bits/stdc++.h>
using namespace std;
long long modexp(long long n, long long m) {
  if (m == 0)
    return 1;
  else if (m == 1)
    return n;
  else {
    long long p = modexp(n, m / 2);
    if (m % 2 == 1)
      return (((p * p) % 1000000007) * n) % 1000000007;
    else
      return (p * p) % 1000000007;
  }
}
long long exp(long long n, long long m) {
  if (m == 0) return 1;
  if (m == 1) return n;
  long long p = exp(n, m / 2);
  if (m % 2 == 1)
    return p * p * n;
  else
    return p * p;
}
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long inv(long long n) { return modexp(n, 1000000007 - 2); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
long long factorial(long long fact[], long long n) {
  fact[0] = 1;
  fact[1] = 1;
  long long prod = 1, i;
  for (i = 2; i < n + 1; i++) {
    prod = (prod * i) % 1000000007;
    fact[i] = prod;
  }
  return prod;
}
bool comparator(pair<long long, long long> p1, pair<long long, long long> p2) {
  return p1.second > p2.second;
}
long long bs(vector<long long> &prime, long long key) {
  long long low = 1, high = (long long)prime.size() - 1, ret = -1;
  while (low <= high) {
    long long mid = (low + high) / 2;
    if (prime[mid] == key)
      return mid;
    else if (key > prime[mid])
      low = mid + 1;
    else
      high = mid - 1;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, i, j, k, m, t, T;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    vector<long long> x(n), y(n);
    for (i = 0; i < n; i++) cin >> x[i];
    for (i = 0; i < n; i++) cin >> y[i];
    sort(x.begin(), x.end());
    vector<long long> mx(n);
    for (i = 0; i < n; i++) {
      long long low = 0, high = n - 1, ret;
      while (low <= high) {
        long long mid = (low + high) / 2;
        long long diff = x[mid] - x[i];
        if (diff <= k) {
          ret = mid;
          low = mid + 1;
        } else
          high = mid - 1;
      }
      mx[i] = ret;
    }
    vector<long long> suff(n);
    suff[n - 1] = mx[n - 1] - (n - 1) + 1;
    for (i = n - 2; i >= 0; i--) suff[i] = max(suff[i + 1], mx[i] - i + 1);
    long long total = 0;
    for (i = 0; i < n; i++) {
      long long sum = mx[i] - i + 1;
      if (mx[i] + 1 < n) sum += suff[mx[i] + 1];
      total = max(total, sum);
    }
    cout << total << endl;
  }
  return 0;
}
