#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  long long f = 1;
  while (!isdigit(ch)) {
    if (ch == '-') f *= -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  x *= f;
}
template <typename T, typename... Args>
void read(T &first, Args &...args) {
  read(first);
  read(args...);
}
template <typename T>
void write(T arg) {
  T x = arg;
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) {
    write(x / 10);
  }
  putchar(x % 10 + '0');
}
template <typename T, typename... Ts>
void write(T arg, Ts... args) {
  write(arg);
  if (sizeof...(args) != 0) {
    putchar(' ');
    write(args...);
  }
}
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int N = 2e5 + 5;
int T, n, k, tot;
int x[N], y[N];
map<int, int> ma;
struct node {
  int val, x;
} p[N];
long long pre[N], sum[N], dis[N], suff[N];
bool check(int r, int l) {
  if (dis[r] - dis[l] < k) return true;
  return false;
}
void DealPre() {
  int l = 1, tmp = 0;
  for (int r = 1; r <= tot; r++) {
    tmp += p[r].val;
    while (p[r].x - p[l].x > k) tmp -= p[l].val, l++;
    pre[r] = tmp;
  }
  for (int i = 1; i <= tot; i++) pre[i] = max(pre[i], pre[i - 1]);
}
void DealSuff() {
  int r = tot, tmp = 0;
  for (int l = tot; l >= 1; l--) {
    tmp += p[l].val;
    while (p[r].x - p[l].x > k) tmp -= p[r].val, r--;
    suff[l] = tmp;
  }
  for (int i = tot; i >= 1; i--) suff[i] = max(suff[i], suff[i + 1]);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> T;
  while (T--) {
    ma.clear();
    cin >> n >> k;
    for (int i = 0; i <= n + 1; i++) pre[i] = suff[i] = 0;
    tot = 0;
    for (int i = 1; i <= n; i++) cin >> x[i], ma[x[i]]++;
    for (int i = 1; i <= n; i++) cin >> y[i];
    for (auto it : ma) {
      p[++tot].x = it.first;
      p[tot].val = it.second;
    }
    DealPre();
    DealSuff();
    long long ans = 0;
    for (int i = 0; i <= tot; i++) ans = max(ans, pre[i] + suff[i + 1]);
    cout << ans << endl;
  }
}
