#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--)
    {
        final_sum = 0;
        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();
            for(i=flag_i;i<=max_border;i++)
            {
                mm[i]=mm[i-1]+m[i];
               // cout<<"m[i] = "<<m[i]<<" mm[i] = "<<mm[i]<<endl;
            }
            flag_i=0;
            for(i=min_border;i<=max_border-k;i++)
            {
                long long int zan = mm[i+k]-mm[i-1];
                max_sum = max(zan,max_sum);
                /*
                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;
        }
        m.clear();
        mm.clear();
        memset(x,0,sizeof x);
        memset(y,0,sizeof y);
    }
    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;i++)
            {
                mm[i]=mm[i-1]+m[i];
                //cout<<"m[i] = "<<m[i]<<" mm[i] = "<<mm[i]<<endl;
            }
            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;
}


#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--)
    {
        final_sum = 0;
        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;i++)
            {
                mm[i]=mm[i-1]+m[i];
               // cout<<"m[i] = "<<m[i]<<" mm[i] = "<<mm[i]<<endl;
            }
            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;
        }
        m.clear();
        mm.clear();
        memset(x,0,sizeof x);
        memset(y,0,sizeof y);
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#include <limits>
#include <algorithm>
using namespace std;
long long int min_border,max_border;
long long int x[200005],y[200005];
int dp[200005],final_sum;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        final_sum = 0;
        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=1;i<=n;i++)
        {
            cin>>x[i];
            min_border = min(min_border,x[i]);
            max_border = max(max_border,x[i]);
        }
        for(i=1;i<=n;i++)
            cin>>y[i];
        if((max_border-min_border)<k*2)
            cout<<n<<endl;
        else
        {
           sort(x+1,x+1+n);

           for(i=n;i>=1;i--) //dp[i]��Ҫ��������¼��벿�ָ��������Ǵ�n��ʼ�� �ڵڶ���for��ʹ��������ǰ�˺�ĩ�˵� �����뿴�ڶ���for
           {
               int mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               int 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]���ó���
           {
               int mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               int 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;
        }
        memset(x,0,sizeof x);
        memset(y,0,sizeof y);
        memset(dp,0,sizeof dp);
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#include <limits>
#include <algorithm>
using namespace std;
long long int x[200005],y[200005];
int dp[200005],final_sum;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        final_sum = 0;
        int n;
        long long int k;
        cin>>n>>k;
        int i,j;
        for(i=1;i<=n;i++)
            cin>>x[i];
        for(i=1;i<=n;i++)
            cin>>y[i];
           sort(x+1,x+1+n);
           for(i=n;i>=1;i--) //dp[i]��Ҫ��������¼��벿�ָ��������Ǵ�n��ʼ�� �ڵڶ���for��ʹ��������ǰ�˺�ĩ�˵� �����뿴�ڶ���for
           {
               int mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               int 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]���ó���
           {
               int mo = x[i]+k;//�ӵ�i��С��ʼ����,��Զ���Ը��ǵ�mo
               int 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;
        memset(x,0,sizeof x);
        memset(y,0,sizeof y);
        memset(dp,0,sizeof dp);
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <cmath>
#include <map>
#include <limits>
#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;
        ll k;
        cin>>n>>k;
        ll i,j;
        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;
        memset(x,0,sizeof x);
        memset(y,0,sizeof y);
        memset(dp,0,sizeof dp);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left <= right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left < right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left < right && r.front() - v[right] <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));

        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left < right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        if(cnt == 20) {
            rep(i, 0, n) {
                cout << v[i] << " ";
            }
            cout << k;
            return 0;
        }
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left < right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left < right && r.front() - v[right] <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left < right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left < right && r.front() - v[right] <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
string to_stri(long long n) {
    string ans = "";
    while(n != 0) {
        ans += (n % 10) + '0';
        n /= 10;
    }
    for (int i = 0; i < ans.size() / 2; i++)
        swap(ans[i], ans[ans.size() - i - 1]);
    return ans;
}
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        if(cnt == 2) {
            string s = "";
            for(int i = 0; i < n; i++) {
                s += to_stri(v[i]) + " ";
            }
            s += to_stri(k);
            cout << s;
            return 0;
        }
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left <= right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    while(t--) {
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && r.front() - r.back() > k) {
                    r.pop();
                }
                ans = max(ans, (int)l.size() +(int)r.size());
                left++;
                right--;
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            if(cnt == 34) {
                rep(i, 0, n) {
                    cout << v[i] << " ";
                }
                cout << k;
                cout << endl;
                return 0;
            }
            // while(true) {
            //     l.push(v[left]);
            //     left++;
            //     if(l.back() - l.front() > k) {
            //         l.pop();
            //         break;
            //     }
            // }
            // while(true) {
            //     r.push(v[right]);
            //     right++;
            //     if(r.front() - r.back() > k) {
            //         r.pop();
            //         break;
            //     }
            // }
            int mx1 = 0, mx2 = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && abs(r.front() - r.back()) > k) {
                    r.pop();
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    while(t--) {
        int n, k; cin >> n >> k;
        vi v(n);
        rep(i, 0, n) {
            cin >> v[i];
        }
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        vi l(n, 0);
        vi r(n, 0);
        int ans = 0;
        int mx1 = 0, mx2 = 0;
        int j = 0;
        for(int i = 0; i < n; i++) {
            while(v[i] - v[j] > k) j++;
            l[i] = i - j + 1;
            if(i > 0) l[i] = max(l[i], l[i - 1]);
        }
        j = n - 1;
        for(int i = n - 1; i >= 0; i--) {
            while(v[j] - v[i] > k) j--;
            r[i] = j - i + 1;
            if(i < n - 1) r[i] = max(r[i], r[i + 1]);
        }
        for(int i = 0; i < n; i++) {
            if(i > 0) {
                if(l[i] == l[i - 1]) {
                    ans = max(ans, l[i] + r[i]);
                }
            }
            ans = max(ans, l[i] + r[i] - 1);
        }
        cout << ans << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    while(t--) {
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            // while(true) {
            //     l.push(v[left]);
            //     left++;
            //     if(l.back() - l.front() > k) {
            //         l.pop();
            //         break;
            //     }
            // }
            // while(true) {
            //     r.push(v[right]);
            //     right++;
            //     if(r.front() - r.back() > k) {
            //         r.pop();
            //         break;
            //     }
            // }
            int mx1 = 0, mx2 = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && r.front() - r.back() > k) {
                    r.pop();
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
// string to_stri(long long n) {
//     string ans = "";
//     while(n != 0) {
//         ans += (n % 10) + '0';
//         n /= 10;
//     }
//     for (int i = 0; i < ans.size() / 2; i++)
//         swap(ans[i], ans[ans.size() - i - 1]);
//     return ans;
// }
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        // if(cnt == 20) {
        //     string s = "";
        //     for(int i = 0; i < n; i++) {
        //         s += to_stri(v[i]) + " ";
        //     }
        //     s += to_stri(k);
        //     cout << s;
        //     return 0;
        // }
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left <= right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));

        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left < right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left < right && r.front() - v[right] <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
string to_stri(long long n) {
    string ans = "";
    while(n != 0) {
        ans += (n % 10) + '0';
        n /= 10;
    }
    for (int i = 0; i < ans.size() / 2; i++)
        swap(ans[i], ans[ans.size() - i - 1]);
    return ans;
}
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        if(cnt == 22) {
            string s = "";
            for(int i = 0; i < n; i++) {
                s += to_stri(v[i]) + " ";
            }
            s += to_stri(k);
            cout << s;
            return 0;
        }
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left <= right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    while(t--) {
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            // while(true) {
            //     l.push(v[left]);
            //     left++;
            //     if(l.back() - l.front() > k) {
            //         l.pop();
            //         break;
            //     }
            // }
            // while(true) {
            //     r.push(v[right]);
            //     right++;
            //     if(r.front() - r.back() > k) {
            //         r.pop();
            //         break;
            //     }
            // }
            int mx1 = 0, mx2 = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && abs(r.front() - r.back()) > k) {
                    r.pop();
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    while(t--) {
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            // while(true) {
            //     l.push(v[left]);
            //     left++;
            //     if(l.back() - l.front() > k) {
            //         l.pop();
            //         break;
            //     }
            // }
            // while(true) {
            //     r.push(v[right]);
            //     right++;
            //     if(r.front() - r.back() > k) {
            //         r.pop();
            //         break;
            //     }
            // }
            int mx1 = 0, mx2 = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && abs(r.front() - r.back()) > k) {
                    r.pop();
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(l.back() - l.front() <= k || r.front() - r.back() <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
string to_stri(long long n) {
    string ans = "";
    while(n != 0) {
        ans += (n % 10) + '0';
        n /= 10;
    }
    for (int i = 0; i < ans.size() / 2; i++)
        swap(ans[i], ans[ans.size() - i - 1]);
    return ans;
}
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        if(cnt == 20) {
            string s = "";
            for(int i = 0; i < n; i++) {
                s += to_stri(v[i]) + " ";
            }
            s += to_stri(k);
            cout << s;
            return 0;
        }
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2 || n == 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            int mx1 = 0, mx2 = 0;
            l.push(v[left]);
            r.push(v[right]);
            while(left < right) {
                l.pop();
                r.pop();
                if(l.empty()) {
                    l.push(v[left]);
                    left++;
                }
                if(r.empty()) {
                    r.push(v[right]);
                    right--;
                }
                while(left <= right && v[left] - l.front() <= k) {
                    l.push(v[left]);
                    left++;
                }

                while(left <= right && abs(r.front() - v[right]) <= k) {
                    r.push(v[right]);
                    right--;
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            ans = mx1 + mx2;
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    ans = max(ans, (int)l.size() + (int)r.size() + 1);
                }
            }
            cout << ans << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define int long long
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define pb push_back
#define rep(i, a, b) for(int i = a; i < b; i++)
#define repr(i, a, b) for(int i = a - 1; i >= b; i--)
#define iter(m) for(auto it = m.begin(); it != m.end(); it++)
#define iterr(m) for(auto it = m.rbegin(); it != m.rend(); it++)
#define N 500500
#define PI 3.14159265358979323846264338327950L
#define speed ios_base :: sync_with_stdio(0);cin.tie(0);cout.tie(0);
signed main(){
    speed;
    int t; cin >> t;
    int cnt = 0;
    while(t--) {
        cnt++;
        int n, k; cin >> n >> k;
        vector<int> v(n);
        rep(i, 0, n) cin >> v[i];
        rep(i, 0, n) {
            int x; cin >> x;
        }
        sort(all(v));
        queue<int> l, r;
        if(v[n - 1] - v[0] <= k * 2) {
            cout << n << endl;
        } else {
            int left = 0, right = n - 1;
            int ans = 0;
            if(cnt == 34) {
                rep(i, 0, n) {
                    cout << v[i] << " ";
                }
                cout << endl;
                return 0;
            }
            // while(true) {
            //     l.push(v[left]);
            //     left++;
            //     if(l.back() - l.front() > k) {
            //         l.pop();
            //         break;
            //     }
            // }
            // while(true) {
            //     r.push(v[right]);
            //     right++;
            //     if(r.front() - r.back() > k) {
            //         r.pop();
            //         break;
            //     }
            // }
            int mx1 = 0, mx2 = 0;
            while(left < right) {
                l.push(v[left]);
                r.push(v[right]);
                while(!l.empty() && abs(l.back() - l.front()) > k) {
                    l.pop();
                }
                while(!r.empty() && abs(r.front() - r.back()) > k) {
                    r.pop();
                }
                mx1 = max(mx1, (int)l.size());
                mx2 = max(mx2, (int)r.size());
                left++;
                right--;
            }
            if(left == right) {
                l.push(v[left]);
                r.push(v[right]);
                if(abs(l.back() - l.front()) <= k || abs(r.front() - r.back()) <= k) {
                    mx1++;
                }
            }
            cout << mx1 + mx2 << endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        print_vector(x_vec);
        sort(x_vec.begin(), x_vec.end());
        print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = 0;
        for(int n = 0; n < N; n++) {

            while( abs(x_vec[n] - x_vec[r]) <= K ) {
                r = min(r+1, N-1);
                if(r == N-1)
                    break;
            }
            if(r == N-1)
                r_vec[n] = r - n + 1;
            else
                r_vec[n] = r - n;

            while( abs(x_vec[n] - x_vec[l]) > K) {
                l = min(l+1, n);
                if(l == N-1)
                    break;
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_vec[n], l_vec[n-1]);
        }

        for(int n = 0; n < N-1; n++) {
            r_sma[n] = max(r_vec[n], r_vec[n+1]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

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

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                r = max(r-1, 0);
                if(r == 0)
                    break;
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            while(abs(x_vec[n] - x_vec[l]) > K) {
                l = min(l+1, n);
                if(l == N-1)
                    break;
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_vec[n], l_vec[n-1]);
        }

        for(int n = 0; n < N-1; n++) {
            r_sma[n] = max(r_vec[n], r_vec[n+1]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        sort(x_vec.begin(), x_vec.end());
        // cout << "x_vec: "; print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                if(r == 0)
                    break;
                r = max(r-1, 0);
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            // cout << "abs(x_vec[" << n << "] - x_vec[" << l << "]): " << abs(x_vec[n] - x_vec[l]) << "\n";
            while(abs(x_vec[n] - x_vec[l]) > K) {
                if(l == N-1)
                    break;
                l = min(l+1, N-1);
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_sma[n-1], l_vec[n]);
        }

        for(int n = N-2; n >= 0; n--) {
            r_sma[n] = max(r_sma[n+1], r_vec[n]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        sort(x_vec.begin(), x_vec.end());
        // cout << "x_vec: "; print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                if(r == 0)
                    break;
                r = max(r-1, 0);
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            // cout << "abs(x_vec[" << n << "] - x_vec[" << l << "]): " << abs(x_vec[n] - x_vec[l]) << "\n";
            while(abs(x_vec[n] - x_vec[l]) > K) {
                if(l == N-1)
                    break;
                l = min(l+1, N-1);
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_sma[n-1], l_vec[n]);
        }

        for(int n = N-2; n >= 0; n--) {
            r_sma[n] = max(r_sma[n+1], r_vec[n]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i] + l_sma[i+1]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        // print_vector(x_vec);
        sort(x_vec.begin(), x_vec.end());
        // print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while( abs(x_vec[n] - x_vec[r]) > K ) {
                r = max(r-1, 0);
                if(r == 0)
                    break;
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            while( abs(x_vec[n] - x_vec[l]) > K) {
                l = min(l+1, n);
                if(l == N-1)
                    break;
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_vec[n], l_vec[n-1]);
        }

        for(int n = 0; n < N-1; n++) {
            r_sma[n] = max(r_vec[n], r_vec[n+1]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        sort(x_vec.begin(), x_vec.end());
        // cout << "x_vec: "; print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                if(r == 0)
                    break;
                r = max(r-1, 0);
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            // cout << "abs(x_vec[" << n << "] - x_vec[" << l << "]): " << abs(x_vec[n] - x_vec[l]) << "\n";
            while(abs(x_vec[n] - x_vec[l]) > K) {
                if(l == N-1)
                    break;
                l = min(l+1, N-1);
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_sma[n-1], l_vec[n]);
        }

        for(int n = N-2; n >= 0; n--) {
            r_sma[n] = max(r_sma[n+1], r_vec[n]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 1; i < N; i++) {
            res = max(res, r_sma[i] + l_sma[i-1]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        sort(x_vec.begin(), x_vec.end());
        // cout << "x_vec: "; print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                if(r == 0)
                    break;
                r = max(r-1, 0);
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            // cout << "abs(x_vec[" << n << "] - x_vec[" << l << "]): " << abs(x_vec[n] - x_vec[l]) << "\n";
            while(abs(x_vec[n] - x_vec[l]) > K) {
                if(l == N-1)
                    break;
                l = min(l+1, N-1);
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_sma[n-1], l_vec[n]);
        }

        for(int n = N-2; n >= 0; n--) {
            r_sma[n] = max(r_sma[n+1], r_vec[n]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i] + l_sma[i-1]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

        // print_vector(x_vec);
        sort(x_vec.begin(), x_vec.end());
        // print_vector(x_vec);

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = 0;
        for(int n = 0; n < N; n++) {

            while( abs(x_vec[n] - x_vec[r]) <= K ) {
                r = min(r+1, N-1);
                if(r == N-1)
                    break;
            }
            if(r == N-1)
                r_vec[n] = r - n + 1;
            else
                r_vec[n] = r - n;

            while( abs(x_vec[n] - x_vec[l]) > K) {
                l = min(l+1, n);
                if(l == N-1)
                    break;
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_vec[n], l_vec[n-1]);
        }

        for(int n = 0; n < N-1; n++) {
            r_sma[n] = max(r_vec[n], r_vec[n+1]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;

void left_just(string &s, int n, char c = ' ') {
    while(s.length() < n) {
        s = c + s;
    }
}

template<typename T> void print_vector(vector<T> &vec) {

    int n = vec.size();
    for(int i = 0; i < n; i++) {
        if(i == n - 1)
            cout << vec[i];
        else
            cout << vec[i] << " ";
    }
    cout << "\n";
}

template<typename T> void print_matrix(vector<vector<T>> &mat) {
    for(int i = 0; i < mat.size(); i++) {
        print_vector(mat[i]);
    }
}

int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;

    for(int t = 0; t < T; t++) {

        int N, K;
        cin >> N >> K;

        vector<int> x_vec(N, 0);
        vector<int> y_vec(N, 0);

        for(int n = 0; n < N; n++)
            cin >> x_vec[n];

        for(int n = 0; n < N; n++)
            cin >> y_vec[n];

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

        vector<int> l_vec(N, 0);
        vector<int> r_vec(N, 0);

        int l = 0;
        int r = N-1;

        for(int n = N-1; n >= 0; n--) {
            while(abs(x_vec[n] - x_vec[r]) > K ) {
                if(r == 0)
                    break;
                r = max(r-1, 0);
            }
            r_vec[n] = r - n + 1;
        }

        for(int n = 0; n < N; n++) {
            while(abs(x_vec[n] - x_vec[l]) > K) {
                if(l == N-1)
                    break;
                l = min(l+1, N-1);
            }
            l_vec[n] = n - l + 1;
        }

        // cout << "l_vec: "; print_vector(l_vec);
        // cout << "r_vec: "; print_vector(r_vec);

        vector<int> l_sma(N, 0);
        vector<int> r_sma(N, 0);
        l_sma[0] = l_vec[0];
        r_sma[N-1] = r_vec[N-1];

        for(int n = 1; n < N; n++) {
            l_sma[n] = max(l_vec[n], l_vec[n-1]);
        }

        for(int n = 0; n < N-1; n++) {
            r_sma[n] = max(r_vec[n], r_vec[n+1]);
        }

        // cout << "l_sma: "; print_vector(l_sma);
        // cout << "r_sma: "; print_vector(r_sma);

        int res = 1;
        for(int i = 0; i < N - 1; i++) {
            res = max(res, r_sma[i+1] + l_sma[i]);
        }

        cout << res << "\n";
    }
}


#include <cstdio>
#include <algorithm>
#include<iomanip>
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <cstring>
#include<stack>

#include <cassert>
#include<map>
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define pb push_back
#define de(x) cerr<<#x<<" = "<<x<<endl
#define __i __int128
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)

#else
#define dbg(...)
#endif
using namespace std;
//using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
const int inf=0x7fffffff;
const int N =5e5+10;
const int maxn = 200010;

void clear(unsigned char *pta, int size )
{//结构体初始化
    while(size>0)
    {
        *pta++ = 0;
        size --;
    }
}

LL   n, k, m ;
LL ar[N],cr[N],dr[N],w[N];
LL i,j,g;set<int >v;

const int maxp = 1e5;

void answer(){

    cin>>n>>m;
    LL ans =0 ;
    LL br[N] ;
    for(int i=1;i<=n;i++)cin >>ar[i];
    for(int i=0;i<n;i++)cin >>k;
    sort(ar+1,ar+1+n );
    for(int i=n;i>=1;i--){
        int s = ar[i]+m;
        j = upper_bound(ar+1,ar+1+n,s)-ar;
        br[i] = max(br[i+1] , j-i);
    }

    for(int i=1 ;i<=n;i++){
        int s = ar[i]+m;
        j = upper_bound(ar+1,ar+1+n,s)-ar;
        ans = max(ans , j-i + br[j]);
    }
    cout<<ans<<endl;

    return ;
}
int main()
{
//    ios::sync_with_stdio(0);
//    cin.tie(0), cout.tie(0);

    int t;cin >>t;
    while(t--)answer();

    return 0;

}


import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		int t = sc.nextInt();

		for (int j = 0; j < t; j++) {
			int n = sc.nextInt();
			int aaa = 0;

			int numOdds = 0;
			int numEvens = 0;
			for (int i = 0; i < n; i++) {
				aaa = sc.nextInt();
				if (aaa % 2 != 0) numOdds++;
				else numEvens++;
			}
			if ((numOdds != 0 && numOdds % 2 == 0 && numEvens > 0) || numOdds % 2 != 0)
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		int t = sc.nextInt();

		for (int j = 0; j < t; j++) {
			int n = sc.nextInt();
			int aaa = 0;

			int numOdds = 0;
			for (int i = 0; i < n; i++) {
				aaa = sc.nextInt();
				if (aaa % 2 != 0) numOdds++;
			}
			if (numOdds % 2 != 0)
				System.out.println("YES");
			else
				System.out.println("NO");
		}
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for (int i = 0; i < n; i++) {
			int odd = 0 , ev = 0 ;
			int p = input.nextInt();
			for (int j = 0; j <p; j++) {
			int x = input.nextInt();

			if (x%2==0) {ev++;}
			else {odd++;}
		}
		 if (odd%2!=0||ev==odd) {

			 System.out.println("YES");
		 }
		 else {
			 System.out.println("NO");
		 }

		}
		input.close();
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for (int i = 0; i < n; i++) {

			int a = input.nextInt();
			int sum =0 ;
			for (int j = 0; j < a; j++) {

				sum+= input.nextInt();

			}

			if (sum %2==0) {
				System.out.println("NO");
			}
			else {
				System.out.println("YES");
			}

		}

		input.close();
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for (int i = 0; i < n; i++) {

			int a = input.nextInt();
			int sum =0 ;
			for (int j = 0; j < a; j++) {

			int x = input.nextInt();
			if (x%2!=0) {
				sum++;
			}
			}

			if (sum %2!=0) {
				System.out.println("YES");
			}
			else {
				System.out.println("NO");
			}

		}

		input.close();
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for (int i = 0; i < n; i++) {

			int a = input.nextInt();
			int sum =0 ;
			for (int j = 0; j < a; j++) {

			int x = input.nextInt();
			sum = sum+x;
			}

			if (sum %2!=0) {
				System.out.println("YES");
			}
			else {
				System.out.println("NO");
			}

		}

		input.close();
	}
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int n = input.nextInt();

		for (int i = 0; i < n; i++) {
			int odd = 0 , ev = 0 ,sum = 0 ;
			int p = input.nextInt();
			for (int j = 0; j <p; j++) {
			int x = input.nextInt();
			sum += x;
			if (x%2==0) {ev++;}
			else {odd++;}
		}
		 if (sum%2!=0||odd>0&&ev>0) {

			 System.out.println("YES");
		 }
		 else {
			 System.out.println("NO");
		 }

		}
		input.close();
	}
}


import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner (System.in);
        int cont = sc.nextInt();
        while(cont > 0){
            int tam = sc.nextInt();
            int[] arr = new int[tam];
            int sum = 0;
            for(int i=0;i<arr.length;i++){
                arr[i] = sc.nextInt();
                sum = sum + arr[i];
            }
            if(sum % 2 != 0) System.out.println("YES");
            else subArr(arr);
            cont--;
        }
    }
    public static void subArr(int[] arr){
        boolean ir = false;
        int pr =0;
        for(int i=0; i<arr.length;i++){
            if(arr[i]% 2 != 0){
                ir = true;
            }else{
                pr++;
            }
        }
        if(ir == true && pr > 0){
            System.out.println("YES");
        }else System.out.println("NO");
    }
}


import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner (System.in);
        int cont = sc.nextInt();
        while(cont > 0){
            int tam = sc.nextInt();
            int[] arr = new int[tam];
            int sum = 0;
            for(int i=0;i<arr.length;i++){
                arr[i] = sc.nextInt();
                sum = sum + arr[i];
            }
            if(sum % 2 != 0) System.out.println("YES");
            else subArr(arr);
            cont--;
        }
    }
    public static boolean isOdd(int[] arr){
        int sum=0;
        for(int i=0;i<arr.length;i++){
            sum = sum + arr[i];
        }
        if(sum % 2 == 0) return false;
        else return true;
    }
    public static void subArr(int[] arr){
        for(int i=0; i<arr.length;i++){
            if(i+1 < arr.length){
                arr[i] = arr[i+1];
            }
            if(isOdd(arr)) System.out.println("YES");
        }
        if(isOdd(arr)) System.out.println("YES");
        else System.out.println("NO");
    }
}


import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner (System.in);
        int cont = sc.nextInt();
        while(cont > 0){
            int tam = sc.nextInt();
            int sum = 0;
            while(tam > 0){
                sum = sum + sc.nextInt();
                tam--;
            }
            if(sum % 2 == 0) System.out.println("NO");
            else System.out.println("YES");
            cont--;
        }
    }
}


import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner (System.in);
        int cont = sc.nextInt();
        while(cont > 0){
            int tam = sc.nextInt();
            int[] arr = new int[tam];
            int sum = 0;
            boolean ir = false;
            int pr =0;
            for(int i=0;i<arr.length;i++){
                arr[i] = sc.nextInt();
                if(arr[i]% 2 != 0) ir = true;
                else pr++;
                sum = sum + arr[i];
            }
            if(sum % 2 != 0) System.out.println("YES");
            else{
                if(ir == true && pr > 0) System.out.println("YES");
                else System.out.println("NO");
            }
            cont--;
        }
    }
}


/**
 * @author : Kshitij
 */
import java.io.*;
import java.util.InputMismatchException;

public class Main {
    public static void main(String[] xps){
        InputReader in = new InputReader(System.in);
        OutputWriter out = new OutputWriter(System.out);
        StringBuilder stringBuilder = new StringBuilder();
        int t=in.readInt();
        while (t-->0){
            int n=in.readInt();
            int[] array=new int[n];
            for (int i = 0; i <n ; i++) {
                array[i]=in.readInt();
            }
            stringBuilder.append(solver(array)).append("\n");
        }
        out.println(stringBuilder);
    }
    public static String solver(int[] array){
        int oddCounter=0;
        boolean even=false;
        for (int a :
                array) {
            if (a%2!=0)
                oddCounter++;
            else
                even=true;
        }
        if ((oddCounter>0 && even)||(oddCounter%2!=0))
            return "YES";
        else
            return "NO";
    }
    private static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public double readDouble() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, readInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, readInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }

    private static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.flush();
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
            writer.flush();
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            writer.flush();
        }
    }
}


/**
 * @author : Kshitij
 */
import java.io.*;
import java.util.InputMismatchException;

public class Main {
    public static void main(String[] xps){
        InputReader in = new InputReader(System.in);
        OutputWriter out = new OutputWriter(System.out);
        StringBuilder stringBuilder = new StringBuilder();
        int t=in.readInt();
        while (t-->0){
            int n=in.readInt();
            int[] array=new int[n];
            for (int i = 0; i <n ; i++) {
                array[i]=in.readInt();
            }
            stringBuilder.append(solver(array)).append("\n");
        }
        out.println(stringBuilder);
    }
    public static String solver(int[] array){
        int oddCounter=0;
        boolean even=false;
        for (int a :
                array) {
            if (a%2!=0)
                oddCounter++;
            else
                even=true;
        }
        if (oddCounter>0 && even)
            return "YES";
        else
            return "NO";
    }
    private static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public double readDouble() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, readInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, readInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }

    private static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.flush();
        }

        public void println(Object... objects) {
            print(objects);
            writer.println();
            writer.flush();
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            writer.flush();
        }
    }
}