#include <bits/stdc++.h>
using namespace std;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lowbit(long long x) { return x & -x; }
const int N = 200010;
int n, k;
map<int, int> mp;
int cnt;
int a[N], sum[N], l[N], r[N], x[N], y;
int get(int x) {
  if (mp.count(x) == 0) mp[x] = ++cnt;
  return mp[x];
}
void init() {
  for (int i = 0; i <= n + 5; ++i) {
    a[i] = sum[i] = l[i] = r[i] = 0;
    x[i] = 0;
  }
  mp.clear();
  cnt = 0;
}
void solve() {
  cin >> n >> k;
  queue<int> q;
  for (int i = 1; i <= n; ++i) scanf("%d", &x[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &y);
  sort(x + 1, x + 1 + n);
  for (int i = 1; i <= n; ++i) {
    if (!q.empty() && q.front() < x[i] - k) q.pop();
    q.push(x[i]);
    l[i] = q.size();
    l[i] = max(l[i], l[i - 1]);
  }
  while (!q.empty()) q.pop();
  for (int i = n; i >= 1; --i) {
    if (!q.empty() && q.front() > x[i] + k) q.pop();
    q.push(x[i]);
    r[i] = q.size();
    r[i] = max(r[i], r[i + 1]);
  }
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    res = max(res, l[i] + r[i + 1]);
  }
  cout << res << '\n';
  init();
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
