#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int d4x[4] = {-1, 0, 1, 0};
const int d4y[4] = {0, 1, 0, -1};
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 7;
const int MAXN = 2e5 + 100;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
void testcase() {
  int n, k;
  cin >> n >> k;
  vector<pair<ll, ll> > a(n);
  map<int, int> mp;
  for (int i = 0; i < (n); ++i) {
    cin >> a[i].first;
    ++mp[a[i].first];
  }
  for (int i = 0; i < (n); ++i) cin >> a[i].second;
  vector<pair<ll, ll> > b;
  vector<int> pre;
  pre.push_back(0);
  for (auto p : mp) {
    b.push_back({p.first, p.second});
    pre.push_back(pre.back() + p.second);
  }
  mp.clear();
  vector<int> cnt;
  for (int i = 0; i + 1 < (ll)b.size(); ++i) {
    int left = b[i].first;
    pair<ll, ll> now = {left + k + 1, -1e9};
    auto it = lower_bound(b.begin(), b.end(), now);
    --it;
    int pos = it - b.begin();
    int tot = pre[pos + 1] - pre[i];
    cnt.push_back(tot);
  }
  cnt.push_back(b.back().second);
  map<int, int> mx;
  int cur = 0;
  for (int i = (ll)cnt.size() - 1; i >= 0; --i) {
    int right = b[i].first;
    cur = max(cur, cnt[i]);
    mx[right] = cur;
  }
  int ans = 0;
  for (int i = 0; i < (ll)cnt.size(); ++i) {
    int right = b[i].first + k;
    int now = 0;
    auto it = mx.upper_bound(right);
    if (it != mx.end()) now = it->second;
    ans = max(cnt[i] + now, ans);
  }
  cout << ans << endl;
}
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  };
  int tt;
  cin >> tt;
  while (tt--) {
    testcase();
  }
  return 0;
}
