#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp >= GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u;
		}
		else if(tmp >= GET(2, ans[1]))
			ans[1] = {i, u, tmp};
	}
	if(GET(2, ans[1]) > 0) {
		ll ret = GET(2, ans[0]) + GET(2, ans[1]);
		if(GET(1, ans[0]) == GET(0, ans[1]))
			ret -= cnt[GET(1, ans[0])].S;
		cout << ret << '\n';
	}
	else
		cout << GET(2, ans[0]) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp > GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u;
		}
		else if(tmp > GET(2, ans[1])) {
			ans[1] = {i, u, tmp};
			//i = u;
		}
	}
	cout << (GET(2, ans[0]) > 0 ? GET(2, ans[0]) : 0) + (GET(2, ans[1]) > 0 ? GET(2, ans[1]) : 0) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp >= GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u;
		}
		else if(tmp >= GET(2, ans[1]))
			ans[1] = {i, u, tmp};
	}
	if(GET(2, ans[1]) > 0) {
		ll ret = GET(2, ans[0]) + GET(2, ans[1]);
		cout << ret << '\n';
	}
	else
		cout << GET(2, ans[0]) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp >= GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u;
		}
		else if(tmp >= GET(2, ans[1])) {
			ans[1] = {i, u, tmp};
			//i = u;
		}
	}
	if(GET(2, ans[1]) > 0) {
		ll ret = GET(2, ans[0]) + GET(2, ans[1]);
		cout << ret << '\n';
	}
	else
		cout << GET(2, ans[0]) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	vector<pii> range(x_idx.size());
	vector<int> ans(x_idx.size());
	vector<int> MAX(x_idx.size());
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		range[i] = {i, u};
		ans[i] = tmp;
	}
	MAX[x_idx.size() - 1] = ans[x_idx.size() - 1];
	for(int i = x_idx.size() - 2; i >= 0; --i) {
		MAX[i] = max(ans[i], MAX[i + 1]);
	}
	ll ret = 0;
	for(int i = 0; i < x_idx.size(); ++i) {
		ll tmp = ans[i];
		if(range[i].S + 1 < x_idx.size())
			tmp += MAX[range[i].S + 1];
		ret = max(tmp, ret);
	}
	cout << ret << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp >= GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u;
		}
		else if(tmp >= GET(2, ans[1])) {
			ans[1] = {i, u, tmp};
			//i = u;
		}
	}
	cout << (GET(2, ans[0]) > 0 ? GET(2, ans[0]) : 0) + (GET(2, ans[1]) > 0 ? GET(2, ans[1]) : 0) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ALL(x) x.begin(), x.end()
#define GET(i, x) get<i>(x)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

const int maxn = 2 * 1e5;
int x[maxn], y[maxn];
inline void solve() {
	int n, k; cin >> n >> k;
	memset(x, 0, sizeof(x));
	memset(y, 0, sizeof(y));
	for(int i = 0; i < n; ++i)
		cin >> x[i];
	for(int i = 0; i < n; ++i)
		cin >> y[i];
	sort(x, x + n);
	vector<pii> cnt;
	vector<int> x_idx;
	for(int i = 0; i < n; ++i) {
		cnt.eb(x[i], 1);
		x_idx.eb(x[i]);
		while(i + 1 < n && x[i] == x[i + 1])
			cnt.back().S++, ++i;
	}
	vector<int> pre_cnt(cnt.size());
	pre_cnt[0] = cnt[0].S;
	for(int i = 1; i < cnt.size(); ++i) {
		pre_cnt[i] = pre_cnt[i - 1] + cnt[i].S;
	}
	tuple<ll, ll, ll> ans[2] = {{-1, -1, -1}, {-1, -1, -1}};
	for(int i = 0; i < x_idx.size(); ++i) {
		auto it = upper_bound(x_idx.begin(), x_idx.end(), x_idx[i] + k);
		int u;
		if(it == x_idx.begin())
			u = 0;
		else
		 	u = prev(it) - x_idx.begin();
		int tmp = pre_cnt[u] - (i - 1 < 0 ? 0 : pre_cnt[i - 1]);
		if(tmp >= GET(2, ans[0])) {
			swap(ans[0], ans[1]);
			ans[0] = {i, u, tmp};
			i = u + 1;
		}
		else if(tmp >= GET(2, ans[1]))
			ans[1] = {i, u, tmp};
	}
	if(GET(2, ans[1]) > 0) {
		ll ret = GET(2, ans[0]) + GET(2, ans[1]);
		cout << ret << '\n';
	}
	else
		cout << GET(2, ans[0]) << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#define ll long long int
using namespace std;
const int MAXN = 2e5+10;
ll t, n, k, x[MAXN], y, ans, dp[MAXN];
struct seg {
    ll c, r;
} s[MAXN];
void get_seg() {
    memset(s, 0, sizeof(s));
    int i = 0, j = 0, m = 0;
    while (i < n) {
        if (j < n && abs(x[j] - x[i]) <= k) {
            s[i].c++;
            j++;
        } else {
            s[i].r = j - 1;
            s[i + 1].c = s[i].c - 1;
            i++;
        }
    }
}
int main() {
    cin >> t;
    while(t--) {
        ans = 0;
        cin >> n >> k;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++) cin >> x[i];
        for (int i = 0; i < n; i++) cin >> y;
        sort(x, x + n);
        get_seg();
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = max(dp[i + 1], s[i].c);
        }
        for (int i = 0; i < n; i++) {
            ans = max(ans, s[i].c + dp[s[i].r + 1]);
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#define ll long long int
using namespace std;
const int MAXN = 2e5+10;
ll t, n, k, x[MAXN], y, ans, dp[MAXN];
struct seg {
    ll c, r;
} s[MAXN];
void get_seg() {
    memset(s, 0, sizeof(s));
    int i = 0, j = 0, m = 0;
    while (i < n) {
        if (j < n && abs(x[j] - x[i]) <= k) {
            s[i].c++;
            j++;
        } else {
            s[i].r = j - 1;
            s[i + 1].c = s[i].c - 1;
            i++;
        }
    }
}
int main() {
    cin >> t;
    while(t--) {
        ans = 0;
        cin >> n >> k;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++) cin >> x[i];
        for (int i = 0; i < n; i++) cin >> y;
        sort(x, x + n);
        get_seg();
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = max(dp[i + 1], s[i].c);
        }
        for (int i = 0; i < n; i++) {
            ans = max(ans, s[i].c + dp[s[i].r + 1]);
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2e5 + 10;
int a[maxn], b[maxn], c[maxn], pre[maxn];
bool com(int a, int b)
{
    return a < b;
}
int qian[maxn], hou[maxn];
signed main()
{
    int t, n, k;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        for (int i = 1; i <= n; i++)
            cin >> b[i];
        sort(a + 1, a + 1 + n, com);
        for (int i = n; i >= 1; i--)
        {
            int nowx = a[i] + k;                                 //从第i个小球开始覆盖,最远可以覆盖到nowx
            int index = upper_bound(a + 1, a + 1 + n, nowx) - a; //刚好大于nowx的索引
            hou[i] = max(hou[i + 1], index - i);                 //取max,因为数组hou[i]的含义是从[i,n]任选一个小球开始覆盖的最优值
        }
        int ans = 0;
        for (int i = 1; i <= n; i++) //枚举第一块木板从i位置开始覆盖
        {
            int nowx = a[i] + k;
            int index = upper_bound(a + 1, a + 1 + n, nowx) - a; //刚好大于nowx的索引
            ans = max(ans, hou[index] + (index - i));
        }
        cout << ans << endl;
        for (int i = 0; i <= n + 1; i++)
            hou[i] = 0;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#define ll long long int
using namespace std;
const int MAXN = 2e4+10;
ll t, n, k, x[MAXN], y, ans;
struct seg {
    ll l, c;
} s[MAXN];
bool cmp(seg a, seg b) {
    if (a.c == b.c) return a.l < b.l;
    else return a.c > b.c;
}
void run() {
    for (int i = 0; i <= n; i++) s[i].l = s[i].c = 0;
    int i = 0, j = 0, m = 0;
    while (j < n) {
        if (abs(x[j] - x[i]) <= k) {
            s[m].c++;
            j++;
        } else {
            s[m].l = i;
            s[m + 1].c = s[m].c - 1;
            i++, m++;
        }
    }
    s[m].l = i;
    s[m].c = n - i;
    /*for (int i = 0; i < n; i++) cout << x[i] << " ";
    cout << endl;
    for (int i = 0; i <= m; i++) {
        cout <<  s[i].l << " " << s[i].c << endl;
    }*/
    sort(s, s + m + 1, cmp);
    ans = max(ans, s[0].c);
    for (i = 1; i <= m; i++) {
        if (abs(x[s[i].l] - x[s[0].l]) <= k) {
            ans = max(ans, s[i].l - s[0].l + s[i].c);
        } else {
            ans = max(ans, s[i].c + s[0].c);
            return;
        }
    }
}
int main() {
    cin >> t;
    while(t--) {
        ans = 0;
        cin >> n >> k;
        for (int i = 0; i < n; i++) cin >> x[i];
        for (int i = 0; i < n; i++) cin >> y;
        sort(x, x + n);
        run();
        reverse(x, x + n);
        run();
        cout << ans << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#define ll long long int
using namespace std;
const int MAXN = 2e4+10;
ll t, n, k, x[MAXN], y, ans, dp[MAXN];
struct seg {
    ll c, r;
} s[MAXN];
void get_seg() {
    memset(s, 0, sizeof(s));
    int i = 0, j = 0, m = 0;
    while (i < n) {
        if (j < n && abs(x[j] - x[i]) <= k) {
            s[i].c++;
            j++;
        } else {
            s[i].r = j - 1;
            s[i + 1].c = s[i].c - 1;
            i++;
        }
    }
}
int main() {
    cin >> t;
    while(t--) {
        ans = 0;
        cin >> n >> k;
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < n; i++) cin >> x[i];
        for (int i = 0; i < n; i++) cin >> y;
        sort(x, x + n);
        get_seg();
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = max(dp[i + 1], s[i].c);
        }
        for (int i = 0; i < n; i++) {
            ans = max(ans, s[i].c + dp[s[i].r + 1]);
        }
        cout << ans << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
#define ll long long int
using namespace std;
const int MAXN = 2e5+10;
ll t, n, k, x[MAXN], y, ans, dp[MAXN];
struct seg {
    ll c, r;
} s[MAXN];
void get_seg() {
    int i = 0, j = 0, m = 0;
    for (int i = 0; i < n; i++) s[i].c = s[i].r = 0;
    while (i < n) {
        if (j < n && abs(x[j] - x[i]) <= k) {
            s[i].c++;
            j++;
        } else {
            s[i].r = j - 1;
            s[i + 1].c = s[i].c - 1;
            i++;
        }
    }
}
int main() {
    cin >> t;
    while(t--) {
        ans = 0;
        cin >> n >> k;
        for (int i = 0; i < n; i++) cin >> x[i];
        for (int i = 0; i < n; i++) cin >> y;
        sort(x, x + n);
        get_seg();
        dp[n] = 0;
        for (int i = n - 1; i >= 0; i--) {
            dp[i] = max(dp[i + 1], s[i].c);
        }
        for (int i = 0; i < n; i++) {
            ans = max(ans, s[i].c + dp[s[i].r + 1]);
        }
        cout << ans << endl;
    }
    return 0;
}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
            int now=xs[i];
            auto it=lower_bound(ALL(xs),now-k);
            // if ( it==xs.begin() ) continue;
            l[i+1]-=l[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
            int now=xs[i];
            auto it=upper_bound(ALL(xs),now+k);
            // if ( it==xs.end() ) continue;
            r[i]-=r[it-xs.begin()];
        }
        for ( int i=0; i<m; i++ ) {
            chmax(l[i+1],l[i]);
        }
        for ( int i=m-1; ~i; i-- ) {
            chmax(r[i],r[i+1]);
        }
        // W(l);
        // W(r);
        int ans=0;
        for ( int i=0; i<m; i++ ) {
            chmax(ans, l[i]+r[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        sort(ALL(X));
        int ans=0;
        VI r(n+1);
        for ( int i=0, j=0; i<n; i++ ) {
            while ( j<n && X[j]-X[i]<=k ) j++;
            r[i]=j-i;
        }
        for ( int i=n-1; i; i-- ) chmax(r[i-1], r[i]);
        for ( int i=0, j=0; i<n; i++ ) {
            while ( j<i && X[i]-X[j]>k ) j++;
            chmax(ans, i-j+1+r[i+1]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#include <bits/stdc++.h>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) {
        int n, k;
        std::cin >> n >> k;
        std::vector<int> x(n), y(n);
        for (int i = 0; i < n; ++i)
            std::cin >> x[i];
        for (int i = 0; i < n; ++i)
            std::cin >> y[i];
        std::sort(x.begin(), x.end());
        std::vector<int> r(n), mx(n + 1);
        int ans = 0;
        for (int i = 0, j = 0; i < n; ++i) {
            while (j < n && x[j] <= x[i] + k)
                ++j;
            r[i] = j;
        }
        for (int i = n - 1; i >= 0; --i)
            mx[i] = std::max(mx[i + 1], r[i] - i);
        for (int i = 0; i < n; ++i)
            ans = std::max(ans, r[i] - i + mx[r[i]]);
        std::cout << ans << "\n";
    }
    return 0;
}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
            int now=xs[i];
            auto it=lower_bound(ALL(xs),now-k);
            // if ( it==xs.begin() ) continue;
            l[i+1]-=l[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
            int now=xs[i];
            auto it=upper_bound(ALL(xs),now+k);
            // if ( it==xs.end() ) continue;
            r[i]-=r[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            chmax(r[i],r[i+1]);
        }
        // W(r);
        int ans=0;
        for ( int i=0; i<=m; i++ ) {
            chmax(ans, l[i]+r[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
            int now=xs[i];
            auto it=lower_bound(ALL(xs),now-k);
            if ( it==xs.begin() ) continue;
            l[i+1]-=l[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
            int now=xs[i];
            auto it=upper_bound(ALL(xs),now+k);
            if ( it==xs.end() ) continue;
            r[i]-=r[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            chmax(r[i],r[i+1]);
        }
        // W(r);
        int ans=0;
        for ( int i=0; i<=m; i++ ) {
            chmax(ans, l[i]+r[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        sort(ALL(X));
        int ans=0,mx=0;
        REP(i,n) {
            int it;
            it=upper_bound(ALL(X), X[i]+k)-X.begin();
            chmax(ans,it-i+mx);
            it=lower_bound(ALL(X),X[i]-k)-X.begin();
            chmax(mx, i-it+1);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
        }
        VI ll(m+1),rr(m+1);
        REP(i,m) {
            ll[i+1]=l[i+1];
            int now=xs[i];
            int it=lower_bound(ALL(xs),now-k)-xs.begin();
            // if ( it==xs.begin() ) continue;
            ll[i+1]-=l[it];
        }
        for ( int i=m-1; ~i; i-- ) {
            rr[i]=r[i];
            int now=xs[i];
            int it=upper_bound(ALL(xs),now+k)-xs.begin();
            // if ( it==xs.end() ) continue;
            rr[i]-=r[it];
        }
        // for ( int i=0; i<m; i++ ) {
        //     chmax(ll[i+1],ll[i]);
        // }
        for ( int i=m-1; ~i; i-- ) {
            chmax(rr[i],rr[i+1]);
        }
        // W(l);
        // W(r);
        int ans=0;
        for ( int i=0; i<m; i++ ) {
            chmax(ans, ll[i]+rr[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
        }
        VI ll(m+1),rr(m+1);
        REP(i,m) {
            ll[i+1]=l[i+1];
            int now=xs[i];
            int it=lower_bound(ALL(xs),now-k)-xs.begin();
            // if ( it==xs.begin() ) continue;
            ll[i+1]-=ll[it];
        }
        for ( int i=m-1; ~i; i-- ) {
            rr[i]=r[i];
            int now=xs[i];
            int it=upper_bound(ALL(xs),now+k)-xs.begin();
            // if ( it==xs.end() ) continue;
            rr[i]-=rr[it];
        }
        // for ( int i=0; i<m; i++ ) {
        //     chmax(ll[i+1],ll[i]);
        // }
        for ( int i=m-1; ~i; i-- ) {
            chmax(rr[i],rr[i+1]);
        }
        // W(l);
        // W(r);
        int ans=0;
        for ( int i=0; i<m; i++ ) {
            chmax(ans, ll[i]+rr[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#pragma region // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }

#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif

template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
    sort(begin(v),end(v),f);
    v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion // }}}

void main() {
    int tt;
    R(tt);
    while ( tt-- ) {
        int n,k;
        R(n,k);
        VI X(n), Y(n);
        REP(i,n) R(X[i]);
        REP(i,n) R(Y[i]);
        VI xs(n);
        REP(i,n) xs[i]=X[i];
        sort_uniq(xs);
        int m=xs.size();
        VI a(m);
        REP(i,n) {
            auto it=lower_bound(ALL(xs),X[i])-xs.begin();
            a[it]++;
        }
        VI l(m+1),r(m+1);
        REP(i,m) {
            l[i+1]=l[i]+a[i];
            int now=xs[i];
            auto it=lower_bound(ALL(xs),now-k);
            // if ( it==xs.begin() ) continue;
            l[i+1]-=l[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            r[i]=r[i+1]+a[i];
            int now=xs[i];
            auto it=upper_bound(ALL(xs),now+k);
            // if ( it==xs.end() ) continue;
            r[i]-=r[it-xs.begin()];
        }
        for ( int i=m-1; ~i; i-- ) {
            chmax(r[i],r[i+1]);
        }
        // W(r);
        LL ans=0;
        for ( int i=0; i<=m; i++ ) {
            chmax(ans, l[i]+r[i]);
        }
        W(ans);
    }
}

}} // {{{ main
int main() { unolight::main(); return 0; }
// }}}


#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int binary (const vector<int> &data, int value) {
	int l = -1, r = data.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (data[m] > value) {
			r = m;
		} else {
			l = m;
		}
	}
	return l;
}

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		// ???
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		map<int, int> count;
		for (int i = 0; i < n; i++) {
			if (count.find(dots[i]) == count.end()) {
				count[dots[i]] = 1;
			} else {
				count[dots[i]]++;
			}
		}

		/*for (auto i = count.begin(); i != count.end(); i++) {
			printf("COUNT %d %d\n", i->first, i->second);
		}*/

		map<int, int> pref;
		pref[dots[0] - 1] = 0;

		for (auto i = count.begin(); i != count.end(); i++) {
			auto j = pref.end();
			j--;
			pref[i->first] = j->second + i->second;
			int prev_pos = i->first - 1;
			if (pref.find(prev_pos) == pref.end()) {
				pref[prev_pos] = j->second;
			}
		}

		/*for (auto i = pref.begin(); i != pref.end(); i++) {
			printf("PREF %d %d\n", i->first, i->second);
		}*/

		map<int, int> dp;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			if (dp.find(dots[j]) == dp.end()) {
				dp[dots[j]] = pref[dots[j]] - pref[dots[i] - 1];
			} else {
				dp[dots[j]] = max(dp[dots[j]], pref[dots[j]] - pref[dots[i] - 1]);
			}
		}

		/*for (auto i = dp.begin(); i != dp.end(); i++) {
			printf("DP %d %d\n", i->first, i->second);
		}*/

		vector<int> pref_max(n);
		vector<int> maxinds(n);
		pref_max[0] = dp[dots[0]];
		maxinds[0] = 0;
		for (int i = 1; i < n; i++) {
			if (dp[dots[i]] >= pref_max[i - 1]) {
				pref_max[i] = dp[dots[i]];
				maxinds[i] = i;
			} else {
				pref_max[i] = pref_max[i - 1];
				maxinds[i] = maxinds[i - 1];
			}

			//printf("%d %d\n", pref_max[i], dots[maxinds[i]]);
		}

		int ans = 0;
		for (int i = 0; i < n; i++) {
			int p1 = dots[i];
			int j = binary(dots, p1 + k);
			int p2 = dots[j];

			//printf("%d %d %d %d %d %d %d\n", p1, p2, pref_max[i], maxinds[i], pref[p1 - 1], pref[p1], pref[p2]);

			if (p1 == dots[maxinds[i]]) {
				//printf("L\n");
				ans = max(ans, pref_max[i] + pref[p2] - pref[p1]);
			} else {
				//printf("R\n");
				ans = max(ans, pref_max[i] + pref[p2] - pref[p1 - 1]);
			}
			//printf("%d\n", ans);
		}

		/*
		int maxind1 = 0, maxind2 = 0, ans_plat1 = 0;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			printf("%d %d %d %d\n", dots[i], dots[j], pref[dots[i] - 1], pref[dots[j]]);
			if (pref[dots[j]] - pref[dots[i] - 1] >= ans_plat1) {
				ans_plat1 = pref[dots[j]] - pref[dots[i] - 1];
				maxind1 = i;
				maxind2 = j;
			}
		}

		//printf("%d %d\n", dots[maxind1], dots[maxind2]);

		int ans = 0;
		for (int i = 1; i < n; i++) {
			if (maxind1 <= i and i <= maxind2) {
				continue;
			}
			int j = binary(dots, dots[i] + k);
			if (maxind1 <= j and j <= maxind2) {
				continue;
			}
			ans = max(ans, pref[dots[j]] - pref[dots[i] - 1]);
		}
		*/
		printf("%d\n", ans);
	}
}


#include <algorithm>
#include <stdio.h>
#include <vector>

using namespace std;

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		// ???
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		int ans = 0;
		for (int i = 0; i < n; i++) {
			int cur_cord1 = dots[i];
			int g = i;
			while (g < n and dots[g] <= cur_cord1 + k) {
				g++;
			}
			g--;
			for (int j = 0; j < n; j++) {
				int cur_cord2 = dots[j];
				int h = j;
				while (h < n and dots[h] <= cur_cord2 + k) {
					h++;
				}
				h--;
				int tmp = 0;
				for (int hh = 0; hh < n; hh++) {
					if ((i <= hh and hh <= g) or (j <= hh and hh <= h)) {
						tmp++;
					}
				}
				//printf("%d %d %d %d %d\n", dots[i], dots[g], dots[j], dots[h], tmp);
				ans = max(ans, tmp);
			}
		}
		printf("%d\n", ans);
	}
}


#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int binary (const vector<int> &data, int value) {
	int l = -1, r = data.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (data[m] > value) {
			r = m;
		} else {
			l = m;
		}
	}
	return l;
}

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		// ???
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		int ans = 0;
		for (int i = 0; i < n; i++) {
			int g = binary(dots, dots[i] + k);
			for (int j = 0; j < n; j++) {
				int h = binary(dots, dots[j] + k);
				int tmp = 0;
				for (int hh = 0; hh < n; hh++) {
					if ((i <= hh and hh <= g) or (j <= hh and hh <= h)) {
						tmp++;
					}
				}
				//printf("%d %d %d %d %d\n", dots[i], dots[g], dots[j], dots[h], tmp);
				ans = max(ans, tmp);
			}
		}
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		vector<int> dp1(n);
		int j = 0;
		for (int i = 0; i < n; i++) {
			while (dots[i] - dots[j] > k) {
				j++;
			}
			dp1[i] = max(dp1[max(0, i - 1)], i - j + 1);
		}
		vector<int> dp2(n);
		j = n - 1;
		for (int i = n - 1; i >= 0; i--) {
			while (dots[j] - dots[i] > k) {
				j--;
			}
			dp2[i] = max(dp2[min(i + 1, n - 1)], j - i + 1);
		}
		int ans = 1;
		for (int i = 0; i < n - 1; i++) {
			ans = max(ans, dp1[i] + dp2[i + 1]);
		}
		printf("%d\n", ans);
	}
}


#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int binary (const vector<int> &data, int value) {
	int l = -1, r = data.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (data[m] > value) {
			r = m;
		} else {
			l = m;
		}
	}
	return l;
}

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		// ???
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		map<int, int> count;
		for (int i = 0; i < n; i++) {
			if (count.find(dots[i]) == count.end()) {
				count[dots[i]] = 1;
			} else {
				count[dots[i]]++;
			}
		}

		/*for (auto i = count.begin(); i != count.end(); i++) {
			printf("COUNT %d %d\n", i->first, i->second);
		}*/

		map<int, int> pref;
		pref[dots[0] - 1] = 0;

		for (auto i = count.begin(); i != count.end(); i++) {
			auto j = pref.end();
			j--;
			pref[i->first] = j->second + i->second;
			int prev_pos = i->first - 1;
			if (pref.find(prev_pos) == pref.end()) {
				pref[prev_pos] = j->second;
			}
		}

		/*for (auto i = pref.begin(); i != pref.end(); i++) {
			printf("PREF %d %d\n", i->first, i->second);
		}*/

		map<int, int> dp;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			if (dp.find(dots[j]) == dp.end()) {
				dp[dots[j]] = pref[dots[j]] - pref[dots[i] - 1];
			} else {
				dp[dots[j]] = max(dp[dots[j]], pref[dots[j]] - pref[dots[i] - 1]);
			}
		}

		/*for (auto i = dp.begin(); i != dp.end(); i++) {
			printf("DP %d %d\n", i->first, i->second);
		}*/

		vector<int> pref_max(n);
		vector<int> maxinds(n);
		pref_max[0] = dp[dots[0]];
		maxinds[0] = 0;
		for (int i = 1; i < n; i++) {
			if (dp[dots[i]] >= pref_max[i - 1]) {
				pref_max[i] = dp[dots[i]];
				maxinds[i] = i;
			} else {
				pref_max[i] = pref_max[i - 1];
				maxinds[i] = maxinds[i - 1];
			}

			//printf("%d %d\n", pref_max[i], dots[maxinds[i]]);
		}

		int ans = 0;
		for (int i = 1; i < n; i++) {
			int p1 = dots[i];
			int j = binary(dots, p1 + k);
			int p2 = dots[j];

			//printf("%d %d %d %d %d %d %d\n", p1, p2, pref_max[i], maxinds[i], pref[p1 - 1], pref[p1], pref[p2]);

			if (p1 == dots[maxinds[i - 1]]) {
				//printf("L\n");
				ans = max(ans, pref_max[i - 1] + pref[p2] - pref[p1]);
			} else {
				//printf("R\n");
				ans = max(ans, pref_max[i - 1] + pref[p2] - pref[p1 - 1]);
			}
			//printf("%d\n", ans);
		}

		/*
		int maxind1 = 0, maxind2 = 0, ans_plat1 = 0;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			printf("%d %d %d %d\n", dots[i], dots[j], pref[dots[i] - 1], pref[dots[j]]);
			if (pref[dots[j]] - pref[dots[i] - 1] >= ans_plat1) {
				ans_plat1 = pref[dots[j]] - pref[dots[i] - 1];
				maxind1 = i;
				maxind2 = j;
			}
		}

		//printf("%d %d\n", dots[maxind1], dots[maxind2]);

		int ans = 0;
		for (int i = 1; i < n; i++) {
			if (maxind1 <= i and i <= maxind2) {
				continue;
			}
			int j = binary(dots, dots[i] + k);
			if (maxind1 <= j and j <= maxind2) {
				continue;
			}
			ans = max(ans, pref[dots[j]] - pref[dots[i] - 1]);
		}
		*/
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		vector<int> dp1(n);
		int j = 0;
		for (int i = 0; i < n; i++) {
			while (j < n - 1 and dots[i] - dots[j + 1] > k) {
				j++;
			}
			dp1[i] = max(dp1[max(0, i - 1)], i - j + 1);
		}
		vector<int> dp2(n);
		j = n - 1;
		for (int i = n - 1; i >= 0; i--) {
			while (j > 0 and dots[j - 1] - dots[i] > k) {
				j--;
			}
			dp2[i] = max(dp2[min(i + 1, n - 1)], j - i + 1);
		}
		int ans = 1;
		for (int i = 0; i < n - 1; i++) {
			ans = max(ans, dp1[i] + dp2[i + 1]);
		}
		printf("%d\n", ans);
	}
}


#include <algorithm>
#include <map>
#include <stdio.h>
#include <vector>

using namespace std;

int binary (const vector<int> &data, int value) {
	int l = -1, r = data.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (data[m] > value) {
			r = m;
		} else {
			l = m;
		}
	}
	return l;
}

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		// ???
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		map<int, int> count;
		for (int i = 0; i < n; i++) {
			if (count.find(dots[i]) == count.end()) {
				count[dots[i]] = 1;
			} else {
				count[dots[i]]++;
			}
		}

		/*for (auto i = count.begin(); i != count.end(); i++) {
			printf("COUNT %d %d\n", i->first, i->second);
		}*/

		map<int, int> pref;
		pref[dots[0] - 1] = 0;

		for (auto i = count.begin(); i != count.end(); i++) {
			auto j = pref.end();
			j--;
			pref[i->first] = j->second + i->second;
			int prev_pos = i->first - 1;
			if (pref.find(prev_pos) == pref.end()) {
				pref[prev_pos] = j->second;
			}
		}

		/*for (auto i = pref.begin(); i != pref.end(); i++) {
			printf("PREF %d %d\n", i->first, i->second);
		}*/

		map<int, int> dp;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			if (dp.find(dots[j]) == dp.end()) {
				dp[dots[j]] = pref[dots[j]] - pref[dots[i] - 1];
			} else {
				dp[dots[j]] = max(dp[dots[j]], pref[dots[j]] - pref[dots[i] - 1]);
			}
		}

		/*for (auto i = dp.begin(); i != dp.end(); i++) {
			printf("DP %d %d\n", i->first, i->second);
		}*/

		vector<int> pref_max(n);
		vector<int> maxinds(n);
		pref_max[0] = dp[dots[0]];
		maxinds[0] = 0;
		for (int i = 1; i < n; i++) {
			if (dp[dots[i]] > pref_max[i - 1]) {
				pref_max[i] = dp[dots[i]];
				maxinds[i] = i;
			} else {
				pref_max[i] = pref_max[i - 1];
				maxinds[i] = maxinds[i - 1];
			}

			//printf("%d %d\n", pref_max[i], maxinds[i]);
		}

		int ans = 0;
		for (int i = 0; i < n; i++) {
			int p1 = dots[i];
			int j = binary(dots, p1 + k);
			int p2 = dots[j];

			//printf("%d %d %d %d %d %d %d\n", p1, p2, pref_max[i], maxinds[i], pref[p1 - 1], pref[p1], pref[p2]);

			if (p1 == dots[maxinds[i]]) {
				//printf("L\n");
				ans = max(ans, pref_max[i] + pref[p2] - pref[p1]);
			} else {
				//printf("R\n");
				ans = max(ans, pref_max[i] + pref[p2] - pref[p1 - 1]);
			}
			//printf("%d\n", ans);
		}

		/*
		int maxind1 = 0, maxind2 = 0, ans_plat1 = 0;
		for (int i = 0; i < n; i++) {
			int j = binary(dots, dots[i] + k);
			printf("%d %d %d %d\n", dots[i], dots[j], pref[dots[i] - 1], pref[dots[j]]);
			if (pref[dots[j]] - pref[dots[i] - 1] >= ans_plat1) {
				ans_plat1 = pref[dots[j]] - pref[dots[i] - 1];
				maxind1 = i;
				maxind2 = j;
			}
		}

		//printf("%d %d\n", dots[maxind1], dots[maxind2]);

		int ans = 0;
		for (int i = 1; i < n; i++) {
			if (maxind1 <= i and i <= maxind2) {
				continue;
			}
			int j = binary(dots, dots[i] + k);
			if (maxind1 <= j and j <= maxind2) {
				continue;
			}
			ans = max(ans, pref[dots[j]] - pref[dots[i] - 1]);
		}
		*/
		printf("%d\n", ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

signed main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, k;
		scanf("%d%d", &n, &k);
		vector<int> dots(n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &dots[i]);
		}
		int y;
		for (int i = 0; i < n; i++) {
			scanf("%d", &y);
		}
		sort(dots.begin(), dots.end());
		vector<int> dp1(n);
		int j = 0;
		for (int i = 0; i < n; i++) {
			while (j < n - 1 and dots[i] - dots[j + 1] > k) {
				j++;
			}
			dp1[i] = max(dp1[max(0, i - 1)], i - j + 1);
		}
		vector<int> dp2(n);
		j = n - 1;
		for (int i = n - 1; i >= 0; i--) {
			while (j > 0 and dots[j - 1] - dots[i] > k) {
				j--;
			}
			dp2[i] = max(dp2[min(i + 1, n - 1)], j - i + 1);
		}
		int ans = 0;
		for (int i = 0; i < n - 1; i++) {
			ans = max(ans, dp1[i] + dp2[i + 1]);
		}
		printf("%d\n", ans);
	}
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<int>v(n), g(n);
        for(int i=0;i<n;i++){
            cin>>v[i];
        }
        for(int i=0;i<n;i++){
            cin>>g[i];
        }

        sort(v.begin(), v.end());
        if(n == 1){
            cout<<1<<endl;
            continue;
        }

        vector<int>pref(n+1), suff(n+1);

        for(int i=0;i<n;i++){
            int cnt = (v.begin() + i) - lower_bound(v.begin(), v.begin()+i, v[i] - k);
            pref[i] = cnt+1;
        }
        for(int i=n-1;i>=0;i--){
            int cnt = upper_bound(v.begin()+i, v.end(), v[i]+k) - (v.begin() + i);
            suff[i] = cnt;
        }

        for(int i=1;i<n;i++){
            pref[i] = max(pref[i-1], pref[i]);
        }
        for(int i=n-2;i>=0;i--){
            suff[i] = max(suff[i+1], suff[i]);
        }
        int ans = max(pref[n-1], suff[0]);
        for(int i=0;i<n-1;i++){
            ans = max(ans, pref[i]+suff[i+1]);
        }
        cout<<ans<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());
        if(n == 1){
            cout<<1<<endl;
            continue;
        }

        vector<int>pref(n+1), suff(n+1);

        for(int i=0;i<n;i++){
            int cnt = 0;
            int j = i;
            while(j>=0 && v[i].first - v[j].first <= k){
                cnt++;
                j--;
            }
            pref[i] = cnt;
            j = i;
            cnt = 0;
            while(j<n && v[j].first - v[i].first <= k){
                cnt++;
                j++;
            }
            suff[i] = cnt;
        }

        for(int i=1;i<n;i++){
            pref[i] = max(pref[i-1], pref[i]);
        }
        for(int i=n-2;i>=0;i--){
            suff[i] = max(suff[i+1], suff[i]);
        }
        int ans = 1;
        for(int i=0;i<n;i++){
            ans = max(ans, pref[i]+suff[i+1]);
        }
        cout<<ans<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1=0, q1=0;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());

        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            ans = max(ans, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(q-p>ans2){
                ans2 = q-p;
                p1 = p, q1 = q-1;
            }
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    if(v[q-1].first - v[p].first == k)
                    p1 = p, q1 = q-1, f = 1;
                    else p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }
        int ans3 = 0;
        if(f == 0){
        // int ans3 = 0;
        p = q = 0;

        while(q<=p1 && p<=p1){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<=p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1;

        while(q<n && p<n){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
    }

    else{
        // int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1-1;

        while(q<n && p<n){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

    }
        cout<<max(ans2 + ans3, ans)<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1=0, q1=0;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());

        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            ans = max(ans, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(q-p>ans2){
                ans2 = q-p;
                p1 = p, q1 = q-1;
            }
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            ans3 = max(ans3, q-p);
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());

        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());
        if(n == 1){
            cout<<1<<endl;
            continue;
        }
        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 1;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());
        if(n == 1){
            cout<<1<<endl;
            continue;
        }
        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 1;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(min(ans2 + ans3, n), ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());
        if(n == 1){
            cout<<1<<endl;
            continue;
        }
        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1, q1;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());

        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize ("-O3")
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define double long double
#define pb push_back
#define F first
#define S second
const int N = 1e6+5;
int mod = 1e9+7;
void pairsort(int a[], int b[], int n){
    pair<int, int> pairt[n];
    for (int i = 0; i < n; i++)
    {
        pairt[i].first = a[i];
        pairt[i].second = b[i];
    }
    sort(pairt, pairt + n);
    for (int i = 0; i < n; i++)
    {
        a[i] = pairt[i].first;
        b[i] = pairt[i].second;
    }
}
ll pow1(ll n,ll p)
{
    if(p==0)    return 1;
    ll x=pow1(n, p/2);
    x=(x*x)%mod;
    if(p%2==0)
        return x;
    else
        return (x*n)%mod;
}
int gcd(int a, int b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
int isPrime(int n){
    if(n < 2)
        return 0;
    if(n < 4)
        return 1;
    if(n % 2 == 0 or n % 3 == 0)
        return 0;
    for(int i = 5; i*i <= n; i += 6)
        if(n % i == 0 or n % (i+2) == 0)
            return 0;
    return 1;
}
long long C(int n, int r) {
    if(r>n-r)
        r=n-r;
    long long ans=1;
    for(int i=1;i<=r;i++){
        ans*=n-r+i;
        ans/=i;
    }
    return ans;
}
bool sortbysec(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second < b.second);
}
bool compare(const pair<ll, ll>&p1, const pair<ll,ll>&p2)
{
    if(p1.F < p2.F) return true;
        if(p1.F == p2.F) return p1.S < p2.S;
        return false;
}

int32_t main()
{
    IOS;
    int tt;
    cin>>tt;
    // tt=1;
    while(tt--){
        int p1=0, q1=0;
        int n,m,k,f=0,flag=0;
        cin>>n>>k;
        int k1 = k;
        vector<pair<int, int >>v(n);
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].first = x;
        }
        for(int i=0;i<n;i++){
            int x;
            cin>>x;
            v[i].second = x;
        }

        sort(v.begin(), v.end());

        int p=0, q=0;
        int ans = 1;
        k*=2;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans = max(ans, q-p);
                p++;
            }
            else p++;
        }
        int ans2 = 1;
        k = k1;
        p = q = 0;
        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                if(q-p>ans2){
                    ans2 = q-p;
                    p1 = p, q1 = q-1;
                }
                p++;
            }
            else p++;
        }

        int ans3 = 0;
        p = q = 0;

        while(q<p1 && p<p1){
            if(v[q].first - v[p].first <= k){
                while(q<p1 && (v[q].first - v[p].first <= k)){
                    q++;
                }

                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }
        p = q = q1+1;

        while(q<n && p<n){
            if(v[q].first - v[p].first <= k){
                while(q<n && (v[q].first - v[p].first <= k)){
                    q++;
                }
                ans3 = max(ans3, q-p);
                p++;
            }
            else p++;
        }

        cout<<max(ans2 + ans3, ans)<<endl;

    }

    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ll long long
using namespace std;
ll x[200005],y[200005];
ll dp[200005],final_sum;
bool cmp(ll a,ll b)
{
    return a<b;
}
int  main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        final_sum = 0;
        ll n,i,k;
        cin>>n>>k;
        for(i=1;i<=n;i++)
            cin>>x[i];
        for(i=1;i<=n;i++)
            cin>>y[i];
           sort(x+1,x+1+n,cmp);
           for(i=n;i>=1;i--) //dp[i]��Ҫ��������¼��벿�ָ��������Ǵ�n��ʼ�� �ڵڶ���for��ʹ��������ǰ�˺�ĩ�˵� �����뿴�ڶ���for
           {
               ll mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               ll ind = upper_bound(x+1,x+1+n,mo)-x;//���ֲ���upper_bound(begin,end,num) ���ر�num��һ�����ֵ���±� ��ȥbegin���ܻ��num���±�
               //cout<<ind<<endl;
               dp[i] = max(dp[i+1],ind-i);//ȡmax,��Ϊ����dp[i]�ĺ����Ǵ�[i,n]��ѡһ��С��ʼ���ǵ�����ֵ
           }
           /*
           ����
            for(i=1;i<=n;i++)
            cout<<dp[i]<<" ";
           cout<<endl;
           */
            for(i=1;i<=n;i++) //���for��1��ʼ ��Ҫ�Ǽ���ǰ���ֵĸ��� ����벿�ֵĸ��� ����dp[i]���ó���
           {
               ll mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               ll ind = upper_bound(x+1,x+1+n,mo)-x;//���ֲ���upper_bound(begin,end,num) ���ر�num��һ�����ֵ���±� ��ȥbegin���ܻ��num���±�
               //cout<<ind<<endl;
               final_sum = max(final_sum,dp[ind]+ind-i);//ȡmax,��Ϊ����dp[i]�ĺ����Ǵ�[i,n]��ѡһ��С��ʼ���ǵ�����ֵ dp[ind]�ǵó� ind֮��ƽ̨��סС�������ֵ ind-i��ind֮ǰ����ֵ
           }
           cout<<final_sum<<endl;
           for(i=1;i<=n;i++)
            dp[i]=0;
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#include <limits>
using namespace std;
long long int min_border,max_border,final_sum;
long long int x[200005],y[200005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        map<long long int ,int> m;
        map<long long int ,long long int> mm;
        int n;
        min_border = numeric_limits<long long int>::max();
        max_border = numeric_limits<long long int>::min();
        long long int k;
        cin>>n>>k;
        int i,j;
        for(i=0;i<n;i++)
        {
            cin>>x[i];
            min_border = min(min_border,x[i]);
            max_border = max(max_border,x[i]);
        }
        for(i=0;i<n;i++)
            cin>>y[i];
        if((max_border-min_border)<k*2)
            cout<<n<<endl;
        else
        {
            for(i=0;i<n;i++)
                m[x[i]]++;
            for(i=min_border;i<=max_border;i++)
            {
                mm[i]=mm[i-1]+m[i];
                //cout<<"m[i] = "<<m[i]<<" mm[i] = "<<mm[i]<<endl;
            }
            long long int max_sum = numeric_limits<long long int>::min();
            int flag_i=0;
            for(i=min_border;i<=max_border-k;i++)
            {
                long long int zan = mm[i+k]-mm[i-1];
                if(max_sum<=zan)
                {
                    max_sum = zan;
                    flag_i = i;
                }
            }
            final_sum += max_sum;
           // cout<<"��һ��final_sum = "<<final_sum<<" ��һ��i = "<<flag_i<<endl;
            mm.clear();
            for(i=flag_i;i<=flag_i+k;i++)
                m[i]=0;
            max_sum = numeric_limits<long long int>::min();
            flag_i=0;
            for(i=min_border;i<=max_border-k;i++)
                mm[i]=mm[i-1]+m[i];
            for(i=min_border;i<=max_border-k;i++)
            {
                long long int zan = mm[i+k]-mm[i-1];
                if(max_sum<=zan)
                {
                    max_sum = zan;
                    flag_i = i;
                }
            }
            final_sum += max_sum;
           // cout<<"�ڶ���final_sum = "<<final_sum<<" �ڶ���i = "<<flag_i<<endl;
            cout<<final_sum<<endl;
        }
    }
    return 0;
}