import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception {
        MyReader reader = new MyReader(System.in);
//        MyReader reader = new MyReader(new FileInputStream("input.txt"));
        MyWriter writer = new MyWriter(System.out);
        new Main().run(reader, writer);
        writer.close();
    }

    private void run(MyReader reader, MyWriter writer) throws Exception {
        int t = reader.nextInt();
//        int t = 1;
        label:
        for (int i = 0; i < t; i++) {
            int n = reader.nextInt();
            int k = reader.nextInt();
            List<Pair<Integer>> list = new ArrayList<>();
            int[] x = reader.nextIntArray(n);
            for (int j = 0; j < n; j++) {
                list.add(new Pair<>(x[j], reader.nextInt()));
            }
            list.sort(Comparator.comparingInt(e -> e.x));
            int l = 0;
            int r = 0;
            int c = 0;
            List<Integer> list2 = new ArrayList<>();
            while (l < n) {
                while (r < n && list.get(r).x - list.get(l).x <= k) {
                    c++;
                    r++;
                }
                list2.add(c--);
                l++;
            }
            int[] max = new int[list2.size() + 1];
            for (int j = list2.size() - 1; j >= 0; j--) {
                max[j] = Math.max(max[j + 1], list2.get(j));
            }
            int ans = 0;
            for (int j = 0; j < list2.size(); j++) {
                int q = list2.get(j) + j < list2.size() ? max[list2.get(j) + j] : 0;
                    ans = Math.max(ans, list2.get(j) + q);
            }
            writer.println(ans);
        }
    }

    class Pair<T> extends Pair2<T, T> {

        public Pair(T x, T y) {
            super(x, y);
        }
    }

    class Pair2<T, U> {

        T x;
        U y;

        public Pair2(T x, U y) {
            this.x = x;
            this.y = y;
        }
    }

    class Triple<T> {
        T x;
        T y;
        T z;

        public Triple(T x, T y, T z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }

    public static class MyReader {

        final BufferedInputStream in;
        final int bufSize = 1 << 16;
        final byte[] buf = new byte[bufSize];
        int i = bufSize;
        int k = bufSize;
        boolean end = false;
        final StringBuilder str = new StringBuilder();

        MyReader(InputStream in) {
            this.in = new BufferedInputStream(in, bufSize);
        }

        int nextInt() throws IOException {
            return (int) nextLong();
        }

        int[] nextIntArray(int n) throws IOException {
            int[] m = new int[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextInt();
            }
            return m;
        }

        int[][] nextIntMatrix(int n, int m) throws IOException {
            int[][] a = new int[n][0];
            for (int j = 0; j < n; j++) {
                a[j] = nextIntArray(m);
            }
            return a;
        }

        long nextLong() throws IOException {
            int c;
            long x = 0;
            int sign = 1;
            while ((c = nextChar()) <= 32) ;
            if (c == '-') {
                sign = -1;
                c = nextChar();
            }
            if (c == '+') {
                c = nextChar();
            }
            while (c >= '0') {
                x = x * 10 + (c - '0');
                c = nextChar();
            }
            return sign * x;
        }

        long[] nextLongArray(int n) throws IOException {
            long[] m = new long[n];
            for (int i = 0; i < n; i++) {
                m[i] = nextLong();
            }
            return m;
        }

        int nextChar() throws IOException {
            if (i == k) {
                k = in.read(buf, 0, bufSize);
                i = 0;
            }
            return i >= k ? -1 : buf[i++];
        }

        String nextString() throws IOException {
            if (end) {
                return null;
            }
            str.setLength(0);
            int c;
            while ((c = nextChar()) <= 32 && c != -1) ;
            if (c == -1) {
                end = true;
                return null;
            }
            while (c > 32) {
                str.append((char) c);
                c = nextChar();
            }
            return str.toString();
        }

        String nextLine() throws IOException {
            if (end) {
                return null;
            }
            str.setLength(0);
            int c = nextChar();
            while (c != '\n' && c != '\r' && c != -1) {
                str.append((char) c);
                c = nextChar();
            }
            if (c == -1) {
                end = true;
                if (str.length() == 0) {
                    return null;
                }
            }
            if (c == '\r') {
                nextChar();
            }
            return str.toString();
        }

        char[] nextCharArray() throws IOException {
            return nextString().toCharArray();
        }

        char[][] nextCharMatrix(int n) throws IOException {
            char[][] a = new char[n][0];
            for (int i = 0; i < n; i++) {
                a[i] = nextCharArray();
            }
            return a;
        }
    }

    public static class MyWriter {

        final BufferedOutputStream out;
        final int bufSize = 1 << 16;
        final byte buf[] = new byte[bufSize];
        int i = 0;
        final byte c[] = new byte[30];
        static final String newLine = System.getProperty("line.separator");

        MyWriter(OutputStream out) {
            this.out = new BufferedOutputStream(out, bufSize);
        }

        void print(long x) throws IOException {
            int j = 0;
            if (i + 30 >= bufSize) {
                flush();
            }
            if (x < 0) {
                buf[i++] = (byte) ('-');
                x = -x;
            }
            do {
                c[j++] = (byte) (x % 10 + '0');
                x /= 10;
            } while (x != 0);
            while (--j >= 0) {
                buf[i++] = c[j];
            }
        }

        void println(long x) throws IOException {
            print(x);
            println();
        }

        void print(int[] m) throws IOException {
            for (int a : m) {
                print(a);
                print(' ');
            }
        }

        void print(long[] m) throws IOException {
            for (long a : m) {
                print(a);
                print(' ');
            }
        }

        void print(String s) throws IOException {
            for (int i = 0; i < s.length(); i++) {
                print(s.charAt(i));
            }
        }

        void print(char x) throws IOException {
            if (i == bufSize) {
                flush();
            }
            buf[i++] = (byte) x;
        }

        void print(char[] m) throws IOException {
            for (char c : m) {
                print(c);
            }
        }

        void println(String s) throws IOException {
            print(s);
            println();
        }

        void println() throws IOException {
            print(newLine);
        }

        void flush() throws IOException {
            out.write(buf, 0, i);
            out.flush();
            i = 0;
        }

        void close() throws IOException {
            flush();
            out.close();
        }
    }
}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[][] points = new int[n][2];
            int[] arr = new int[n];
            for(int j = 0; j < 2; ++j) {
                for(int p = 0; p < n; ++p) {
                    points[p][j] = scanner.nextInt();
                    if (j == 0) {
                        arr[p] = points[p][0];
                    }
                }
            }

            int ret = solve(arr, k);
            System.out.println(ret);
        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

    public static void solve_wrong(int[] arr, int k) {
        Arrays.sort(arr);

        int[] diffTable = new int[arr.length];
        int maxdiffIndex = 0;
        int[] nextTable = new int[arr.length];

        for(int i = 0; i < arr.length; ++i) {
            // biggest courage
            int next = arr[i] + k;

            // find the max index that can be covered
            int index = Arrays.binarySearch(arr, i+1, arr.length, next);
            if(index < 0) {
                index = -index -2;
            }

            int diff = index - i + 1;
            diffTable[i] = diff;

            // update max diff index
            if(diff > diffTable[maxdiffIndex]) {
                maxdiffIndex = i;
            }
        }

        // overlap max target
        int maxtarget = maxdiffIndex + diffTable[maxdiffIndex] -1;
        int maxtargetIndex = maxdiffIndex;
        for(int i = maxdiffIndex+1; i < arr.length; ++i) {
            if(i + diffTable[i] - 1 > maxtarget) {
                maxtarget = i + diffTable[i] - 1;
                maxtargetIndex = i;
            }
        }

//        for(int i = 0; i < diffTable.length; ++i) {
//            System.out.print(diffTable[i] + " ");
//        }
//        System.out.println();

    }
}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] x = new int[n];
            int[] y = new int[n];
            for(int p = 0; p < n; ++p) {
                x[p] = scanner.nextInt();
            }
            for(int p = 0; p < n; ++p) {
                y[p] = scanner.nextInt();
            }

            if(k == 98765) {
                System.out.println("now to run big test");
                System.out.println("n=" +  n + ", x.length=" + x.length + ", k=" + k);
            }

            int ret = solve(x, k);
            System.out.println(ret);

        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            Integer[] x = new Integer[n];
            int[] y = new int[n];
            for(int p = 0; p < n; ++p) {
                x[p] = scanner.nextInt();
            }
            for(int p = 0; p < n; ++p) {
                y[p] = scanner.nextInt();
            }

            int ret = solve(x, k);
            System.out.println(ret);

        }
    }

    public static int solve(Integer[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[][] points = new int[n][2];
            int[] arr = new int[n];
            for(int j = 0; j < 2; ++j) {
                for(int p = 0; p < n; ++p) {
                    points[p][j] = scanner.nextInt();
                    if (j == 0) {
                        arr[p] = points[p][0];
                    }
                }
            }

            int ret = solve(arr, k);
            System.out.println(ret);
        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] x = new int[n];
            int[] y = new int[n];
            for(int p = 0; p < n; ++p) {
                x[p] = scanner.nextInt();
            }
            for(int p = 0; p < n; ++p) {
                y[p] = scanner.nextInt();
            }

            if(k == 98765) {
                System.out.println("now to run big test");
            }

            int ret = solve(x, k);
            System.out.println(ret);

        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[][] points = new int[n][2];
            int[] arr = new int[n];
            for(int j = 0; j < 2; ++j) {
                for(int p = 0; p < n; ++p) {
                    points[p][j] = scanner.nextInt();
                    if (j == 0) {
                        arr[p] = points[p][0];
                    }
                }
            }

            System.out.println("start");
            long start = System.currentTimeMillis();

            int ret = solve(arr, k, start);
            System.out.println(ret);

            long duration = System.currentTimeMillis() - start;
            System.out.println("duration(ms)=" + duration);
        }
    }

    public static int solve(int[] nums, int k, long start) {
        Arrays.sort(nums);
        int n = nums.length;

        System.out.println("#1-duration(ms)=" + (System.currentTimeMillis() - start));

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] x = new int[n];
            int[] y = new int[n];
            for(int p = 0; p < n; ++p) {
                x[p] = scanner.nextInt();
            }
            for(int p = 0; p < n; ++p) {
                y[p] = scanner.nextInt();
            }

            if(k == 98765) {
                System.out.println("now to run big test");
                System.out.println("n=" +  n + ", x.length=" + x.length + ", k=" + k);
            }

            int ret = solve(x, k);
            System.out.println(ret);

        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        if(k == 98765) {
            System.out.println("nums[0]=" + nums[0] + ", nums[n-1]=" + nums[n-1]);
        }

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[][] points = new int[n][2];
            int[] arr = new int[n];
            for(int j = 0; j < 2; ++j) {
                for(int p = 0; p < n; ++p) {
                    points[p][j] = scanner.nextInt();
                    if (j == 0) {
                        arr[p] = points[p][0];
                    }
                }
            }

//            System.out.println("start");
            long start = System.currentTimeMillis();

            int ret = solve(arr, k, start);
            System.out.println(ret);

//            long duration = System.currentTimeMillis() - start;
//            System.out.println("duration(ms)=" + duration);
        }
    }

    public static int solve(int[] nums, int k, long start) {
        Arrays.sort(nums);
        int n = nums.length;

        if(k == 98765) {
            System.out.println("#1-duration(ms)=" + (System.currentTimeMillis() - start));
        }

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[][] points = new int[n][2];
            int[] arr = new int[n];
            for(int j = 0; j < 2; ++j) {
                for(int p = 0; p < n; ++p) {
                    points[p][j] = scanner.nextInt();
                    if (j == 0) {
                        arr[p] = points[p][0];
                    }
                }
            }

            System.out.println("start");
            long start = System.currentTimeMillis();

            int ret = solve(arr, k, start);
            System.out.println(ret);

            long duration = System.currentTimeMillis() - start;
            System.out.println("duration(ms)=" + duration);
        }
    }

    public static int solve(int[] nums, int k, long start) {
        Arrays.sort(nums);
        int n = nums.length;

        if(k == 98765) {
            System.out.println("#1-duration(ms)=" + (System.currentTimeMillis() - start));
        }

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class CF_Round_667_Div3_ProblemE_TwoPlatforms {
    public static void main(String[] args) {
        process();
    }

    public static void process() {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for(int i = 0; i < t; ++i) {
            int n = scanner.nextInt();
            int k = scanner.nextInt();
            int[] x = new int[n];
            int[] y = new int[n];
            for(int p = 0; p < n; ++p) {
                x[p] = scanner.nextInt();
            }
            for(int p = 0; p < n; ++p) {
                y[p] = scanner.nextInt();
            }

            int ret = solve(x, k);
            System.out.println(ret);

        }
    }

    public static int solve(int[] nums, int k) {
        Arrays.sort(nums);
        int n = nums.length;

        int[] left = new int[n];
        int[] right = new int[n];
        for(int i = 0, j = 0; i < n; ++i) {
            while(nums[i] - nums[j] > k) {
                j++;
            }
            left[i] = i-j+1;
            if(i > 0) {
                left[i] = Math.max(left[i], left[i-1]);
            }
        }
        for(int i = n-1, j = n-1; i >= 0; --i) {
            while(nums[j] - nums[i] > k) {
                j--;
            }
            right[i] = j - i + 1;
            if(i < n-1) {
                right[i]  = Math.max(right[i], right[i+1]);
            }
        }
        int ret = 1;
        for(int i = 0; i < n-1; ++i) {
            ret = Math.max(ret, left[i] + right[i+1]);
        }
        return ret;
    }

}


import java.math.BigInteger;
import java.util.*;
import java.io.*;

public class Main {
    static int n,k;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        int t = sc.nextInt();
        while (t-->0) {
            n = sc.nextInt();
            k = sc.nextInt();
            Integer[] a = new Integer[n];
            for (int i = 0; i < n; i++)
                a[i] = sc.nextInt();
            for (int i = 0; i < n; i++)
                sc.nextInt();
            Arrays.sort(a);

            int[] dp = new int[n + 1];
            Arrays.fill(dp, 1);
            int i = 0;
            int j = 0;
            while (i<n) {
                if (j==n||(a[j] - a[i] > k)) {
                    if (i > 0)
                        dp[i] = Math.max(dp[i - 1] - 1, dp[i]);

                    i++;
                } else {
                    dp[i] = Math.max(dp[i], j - i + 1);
                    j++;
                }
            }
            int[]max= new int[n+1];
            max[n-1]=dp[n-1];
            for (i=n-2;i>=0;i--){
                max[i]=Math.max(dp[i],max[i+1]);
            }
            int ans=0;
//            System.err.println(Arrays.toString(dp));
            for (i=0;i<n;i++){
                ans=Math.max(ans,dp[i]+max[i+dp[i]]);
            }
            pw.println(ans);
        }
        pw.flush();
    }
    static class Scanner {
        BufferedReader br;
        StringTokenizer st;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public Scanner(FileReader f) {
            br = new BufferedReader(f);
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public double nextDouble() throws IOException {
            return Double.parseDouble(next());
        }

        public int[] nextIntArr(int n) throws IOException {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = Integer.parseInt(next());
            }
            return arr;
        }

    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.*;
import java.util.*;

public class TwoPlatforms {
    // Radix sort Java implementation

    private static class Radix {

        // A utility function to get maximum value in arr[]
        static int getMax(int arr[], int n)
        {
            int mx = arr[0];
            for (int i = 1; i < n; i++)
                if (arr[i] > mx)
                    mx = arr[i];
            return mx;
        }

        // A function to do counting sort of arr[] according to
        // the digit represented by exp.
        static void countSort(int arr[], int n, int exp)
        {
            int output[] = new int[n]; // output array
            int i;
            int count[] = new int[10];
            Arrays.fill(count,0);

            // Store count of occurrences in count[]
            for (i = 0; i < n; i++)
                count[ (arr[i]/exp)%10 ]++;

            // Change count[i] so that count[i] now contains
            // actual position of this digit in output[]
            for (i = 1; i < 10; i++)
                count[i] += count[i - 1];

            // Build the output array
            for (i = n - 1; i >= 0; i--)
            {
                output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
                count[ (arr[i]/exp)%10 ]--;
            }

            // Copy the output array to arr[], so that arr[] now
            // contains sorted numbers according to curent digit
            for (i = 0; i < n; i++)
                arr[i] = output[i];
        }

        // The main function to that sorts arr[] of size n using
        // Radix Sort
        static void radixsort(int arr[], int n)
        {
            // Find the maximum number to know number of digits
            int m = getMax(arr, n);

            // Do counting sort for every digit. Note that instead
            // of passing digit number, exp is passed. exp is 10^i
            // where i is current digit number
            for (int exp = 1; m/exp > 0; exp *= 10)
                countSort(arr, n, exp);
        }

        // A utility function to print an array
        static void print(int arr[], int n)
        {
            for (int i=0; i<n; i++)
                System.out.print(arr[i]+" ");
        }
    }
    /* This code is contributed by Devesh Agrawal */

    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }
//
//    private static int solve(int n, int k, int[] xs) {
//        Arrays.sort(xs); // 1 1 2 3 4 5 5
//        // find maximum location after i
//        int[] js = new int[n];
//        for (int i = 0; i < n; ++i) {
//            int j = i;
//            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, xs[i] + k);
//            j = -j -1;
//            js[i] = j;
//        }
//        int[] maxima = new int[n];
//        maxima[n-1] = 1;
//        for (int i = n-2; i >= 0; --i)
//            maxima[i] = Math.max(maxima[i+1], js[i] - i);
//        // place both platforms
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < n; ++i) {
//            if (js[i] >= n) {
//                max = Math.max(max, js[i] - i);
//                break;
//            } else {
//                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
//            }
//        }
//        return max;
//    }

    private static int solve(int n, int k, int[] xs) {
        if (xs.length < 3) return xs.length;
        Radix.radixsort(xs, xs.length);
        int[] is = new int[n];
        for (int i = 0, j = 0; i < n; ++j) {
            while (xs[j] - xs[i] > k) ++i;
            is[j] = i;
        }
        int[] maxima = new int[n];
        maxima[0] = 1;
        for (int j = 1; j < n; ++j)
            maxima[j] = Math.max(maxima[j-1], j - is[j] + 1);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int j = n-1; j >= 0; --j) {
            if (is[j] - 1 < 0) {
                max = Math.max(max, j - is[j] + 1);
                break;
            } else {
                max = Math.max(max, (j - is[j] + 1) + maxima[is[j] - 1]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }

    private static int solve(int n, int k, int[] xs) {
        Arrays.sort(xs); // 1 1 2 3 4 5 5
        // find maximum location after i
        int[] js = new int[n];
        for (int i = 0; i < n; ++i) {
            int j = i;
            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, xs[i] + k);
            j = -j -1;
            js[i] = j;
        }
        int[] maxima = new int[n];
        maxima[n-1] = 1;
        for (int i = n-2; i >= 0; --i)
            maxima[i] = Math.max(maxima[i+1], js[i] - i);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            if (js[i] >= n) {
                max = Math.max(max, js[i] - i);
                break;
            } else {
                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.*;
import java.util.*;

public class TwoPlatforms {
    // Radix sort Java implementation

    private static class Radix {

        // A utility function to get maximum value in arr[]
        static int getMax(int arr[], int n)
        {
            int mx = arr[0];
            for (int i = 1; i < n; i++)
                if (arr[i] > mx)
                    mx = arr[i];
            return mx;
        }

        // A function to do counting sort of arr[] according to
        // the digit represented by exp.
        static void countSort(int arr[], int n, int exp)
        {
            int output[] = new int[n]; // output array
            int i;
            int count[] = new int[10];
            Arrays.fill(count,0);

            // Store count of occurrences in count[]
            for (i = 0; i < n; i++)
                count[ (arr[i]/exp)%10 ]++;

            // Change count[i] so that count[i] now contains
            // actual position of this digit in output[]
            for (i = 1; i < 10; i++)
                count[i] += count[i - 1];

            // Build the output array
            for (i = n - 1; i >= 0; i--)
            {
                output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
                count[ (arr[i]/exp)%10 ]--;
            }

            // Copy the output array to arr[], so that arr[] now
            // contains sorted numbers according to curent digit
            for (i = 0; i < n; i++)
                arr[i] = output[i];
        }

        // The main function to that sorts arr[] of size n using
        // Radix Sort
        static void radixsort(int arr[], int n)
        {
            // Find the maximum number to know number of digits
            int m = getMax(arr, n);

            // Do counting sort for every digit. Note that instead
            // of passing digit number, exp is passed. exp is 10^i
            // where i is current digit number
            for (int exp = 1; m/exp > 0; exp *= 10)
                countSort(arr, n, exp);
        }

        // A utility function to print an array
        static void print(int arr[], int n)
        {
            for (int i=0; i<n; i++)
                System.out.print(arr[i]+" ");
        }

        /*Driver function to check for above function*/
        public static void main (String[] args)
        {
            int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
            int n = arr.length;
            radixsort(arr, n);
            print(arr, n);
        }
    }
    /* This code is contributed by Devesh Agrawal */

    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }
//
//    private static int solve(int n, int k, int[] xs) {
//        Arrays.sort(xs); // 1 1 2 3 4 5 5
//        // find maximum location after i
//        int[] js = new int[n];
//        for (int i = 0; i < n; ++i) {
//            int j = i;
//            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, xs[i] + k);
//            j = -j -1;
//            js[i] = j;
//        }
//        int[] maxima = new int[n];
//        maxima[n-1] = 1;
//        for (int i = n-2; i >= 0; --i)
//            maxima[i] = Math.max(maxima[i+1], js[i] - i);
//        // place both platforms
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < n; ++i) {
//            if (js[i] >= n) {
//                max = Math.max(max, js[i] - i);
//                break;
//            } else {
//                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
//            }
//        }
//        return max;
//    }

    private static int solve(int n, int k, int[] xs) {
        if (xs.length < 3) return xs.length;
        Radix.radixsort(xs, xs.length);
        int[] is = new int[n];
        for (int i = 0, j = 0; i < n; ++j) {
            while (xs[j] - xs[i] > k) ++i;
            is[j] = i;
        }
        int[] maxima = new int[n];
        maxima[0] = 1;
        for (int j = 1; j < n; ++j)
            maxima[j] = Math.max(maxima[j-1], j - is[j] + 1);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int j = n-1; j >= 0; --j) {
            if (is[j] - 1 < 0) {
                max = Math.max(max, j - is[j] + 1);
                break;
            } else {
                max = Math.max(max, (j - is[j] + 1) + maxima[is[j] - 1]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }

    private static int solve(int n, int k, int[] xs) {
        Arrays.sort(xs);
        // find maximum location after i
        int[] js = new int[n];
        for (int i = 0; i < n; ++i) {
            int j = i;
            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, i + k);
            j = -j -1;
            js[i] = j;
        }
        int[] maxima = new int[n];
        maxima[n-1] = 1;
        for (int i = n-2; i >= 0; --i)
            maxima[i] = Math.max(maxima[i+1], js[i] - i);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            if (js[i] >= n) continue;
            max = Math.max(max, (js[i] - i) + maxima[js[i]]);
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }

    private static int solve(int n, int k, int[] xs) {
        Arrays.sort(xs);
        // find maximum location after i
        int[] js = new int[n];
        for (int i = 0; i < n; ++i) {
            int j = Arrays.binarySearch(xs, i + k - 1);
            if (j < 0) j = -j - 1;
            js[i] = j;
        }
        int[] maxima = new int[n];
        maxima[n-1] = 1;
        for (int i = n-2; i >= 0; --i)
            maxima[i] = Math.max(maxima[i+1], js[i] - i);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            max = Math.max(max, (js[i] - i) + maxima[js[i]]);
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.TreeMap;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }
//
//    private static int solve(int n, int k, int[] xs) {
//        Arrays.sort(xs); // 1 1 2 3 4 5 5
//        // find maximum location after i
//        int[] js = new int[n];
//        for (int i = 0; i < n; ++i) {
//            int j = i;
//            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, xs[i] + k);
//            j = -j -1;
//            js[i] = j;
//        }
//        int[] maxima = new int[n];
//        maxima[n-1] = 1;
//        for (int i = n-2; i >= 0; --i)
//            maxima[i] = Math.max(maxima[i+1], js[i] - i);
//        // place both platforms
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < n; ++i) {
//            if (js[i] >= n) {
//                max = Math.max(max, js[i] - i);
//                break;
//            } else {
//                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
//            }
//        }
//        return max;
//    }

    private static int solve(int n, int k, int[] xs) {
        if (xs.length < 3) return xs.length;
        Radix.radixsort(xs, xs.length);
        int[] is = new int[n];
        for (int i = 0, j = 0; i < n; ++j) {
            while (xs[j] - xs[i] > k) ++i;
            is[j] = i;
        }
        int[] maxima = new int[n];
        maxima[0] = 1;
        for (int j = 1; j < n; ++j)
            maxima[j] = Math.max(maxima[j-1], j - is[j] + 1);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int j = n-1; j >= 0; --j) {
            if (is[j] - 1 < 0) {
                max = Math.max(max, j - is[j] + 1);
                break;
            } else {
                max = Math.max(max, (j - is[j] + 1) + maxima[is[j] - 1]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.io.*;
import java.util.*;

public class TwoPlatforms {
    // Radix sort Java implementation

    private static class Radix {

        // A utility function to get maximum value in arr[]
        static int getMax(int arr[], int n)
        {
            int mx = arr[0];
            for (int i = 1; i < n; i++)
                if (arr[i] > mx)
                    mx = arr[i];
            return mx;
        }

        // A function to do counting sort of arr[] according to
        // the digit represented by exp.
        static void countSort(int arr[], int n, int exp)
        {
            int output[] = new int[n]; // output array
            int i;
            int count[] = new int[10];
            Arrays.fill(count,0);

            // Store count of occurrences in count[]
            for (i = 0; i < n; i++)
                count[ (arr[i]/exp)%10 ]++;

            // Change count[i] so that count[i] now contains
            // actual position of this digit in output[]
            for (i = 1; i < 10; i++)
                count[i] += count[i - 1];

            // Build the output array
            for (i = n - 1; i >= 0; i--)
            {
                output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
                count[ (arr[i]/exp)%10 ]--;
            }

            // Copy the output array to arr[], so that arr[] now
            // contains sorted numbers according to curent digit
            for (i = 0; i < n; i++)
                arr[i] = output[i];
        }

        // The main function to that sorts arr[] of size n using
        // Radix Sort
        static void radixsort(int arr[], int n)
        {
            // Find the maximum number to know number of digits
            int m = getMax(arr, n);

            // Do counting sort for every digit. Note that instead
            // of passing digit number, exp is passed. exp is 10^i
            // where i is current digit number
            for (int exp = 1; m/exp > 0; exp *= 10)
                countSort(arr, n, exp);
        }

        // A utility function to print an array
        static void print(int arr[], int n)
        {
            for (int i=0; i<n; i++)
                System.out.print(arr[i]+" ");
        }
    }
    /* This code is contributed by Devesh Agrawal */

    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }
//
//    private static int solve(int n, int k, int[] xs) {
//        Arrays.sort(xs); // 1 1 2 3 4 5 5
//        // find maximum location after i
//        int[] js = new int[n];
//        for (int i = 0; i < n; ++i) {
//            int j = i;
//            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, xs[i] + k);
//            j = -j -1;
//            js[i] = j;
//        }
//        int[] maxima = new int[n];
//        maxima[n-1] = 1;
//        for (int i = n-2; i >= 0; --i)
//            maxima[i] = Math.max(maxima[i+1], js[i] - i);
//        // place both platforms
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < n; ++i) {
//            if (js[i] >= n) {
//                max = Math.max(max, js[i] - i);
//                break;
//            } else {
//                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
//            }
//        }
//        return max;
//    }

    private static int solve(int n, int k, int[] xs) {
        if (xs.length < 3) return xs.length;
        Radix.radixsort(xs, xs.length);
        int[] is = new int[n];
        for (int i = 0, j = 0; j < n; ++j) {
            while (xs[j] - xs[i] > k) ++i;
            is[j] = i;
        }
        int[] maxima = new int[n];
        maxima[0] = 1;
        for (int j = 1; j < n; ++j)
            maxima[j] = Math.max(maxima[j-1], j - is[j] + 1);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int j = n-1; j >= 0; --j) {
            if (is[j] - 1 < 0) {
                max = Math.max(max, j - is[j] + 1);
                break;
            } else {
                max = Math.max(max, (j - is[j] + 1) + maxima[is[j] - 1]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }

    private static int solve(int n, int k, int[] xs) {
        Arrays.sort(xs);
        // find maximum location after i
        int[] js = new int[n];
        for (int i = 0; i < n; ++i) {
            int j = i;
            while (j >= 0) j = Arrays.binarySearch(xs, j+1, n, i + k);
            j = -j -1;
            js[i] = j;
        }
        int[] maxima = new int[n];
        maxima[n-1] = 1;
        for (int i = n-2; i >= 0; --i)
            maxima[i] = Math.max(maxima[i+1], js[i] - i);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            if (js[i] >= n) {
                max = Math.max(max, js[i] - i);
                break;
            } else {
                max = Math.max(max, (js[i] - i) + maxima[js[i]]);
            }
        }
        return max;
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class TwoPlatforms {
    public static void main(String[] args) throws IOException {
        try (BufferedReader console = new BufferedReader(new InputStreamReader(System.in))) {
            int t = Integer.parseInt(console.readLine());
            for (int i = 0; i < t; ++i) {
                String[] ab = console.readLine().split(" ");
                int n = Integer.parseInt(ab[0]);
                int k = Integer.parseInt(ab[1]);
                String[] sxs = console.readLine().split(" ");
                int[] xs = new int[n];
                for (int j = 0; j < n; ++j)
                    xs[j] = Integer.parseInt(sxs[j]);
                console.readLine(); // discard ys
                System.out.println(solve(n,k,xs));
            }

        }
    }

    private static int solve(int n, int k, int[] xs) {
        Arrays.sort(xs);
        // find maximum location after i
        int[] js = new int[n];
        for (int i = 0; i < n; ++i) {
            int j = Arrays.binarySearch(xs, i + k - 1);
            if (j < 0) j = -j - 1;
            js[i] = j;
        }
        int[] maxima = new int[n];
        maxima[n-1] = 1;
        for (int i = n-2; i >= 0; --i)
            maxima[i] = Math.max(maxima[i+1], js[i] - i);
        // place both platforms
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            if (js[i] >= n) continue;
            max = Math.max(max, (js[i] - i) + maxima[js[i]]);
        }
        return max;
    }

}


/**
 * BaZ :D
 */

import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class Main
{
    static MyScanner scan;
    static PrintWriter pw;
    static long MOD = 1_000_000_007;
    static long INF = 2_000_000_000_000_000_000L;
    static long inf = 2_000_000_000;
    public static void main(String[] args) {
        new Thread(null, null, "BaZ", 1 << 27) {
            public void run() {
                try {
                    solve();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }.start();
    }

    static void solve() throws IOException
    {
        //initIo(true, "");
        initIo(false, "");
        StringBuilder sb = new StringBuilder();
        int t = ni();
        while (t-->0) {
            int n = ni(), k = ni();
            HashMap<Integer, Integer> map = new HashMap<>();
            for(int i=0;i<n;i++) {
                add(map, ni());
            }
            for(int i=0;i<n;i++) {
                ni();
            }
            int sz = map.size();
            Integer arr[] = new Integer[sz];
            int idx = 0;
            for(int e : map.keySet()) {
                arr[idx++] = e;
            }
            Arrays.sort(arr);
            int pref[] = new int[sz];
            for(int i=0;i<sz;i++) {
                pref[i] = map.get(arr[i]);
                if(i>0) {
                    pref[i] += pref[i - 1];
                }
            }
            int dp[] = new int[sz];
            int till[] = new int[sz];
            int right = -1;
            for(int i=0;i<sz;i++) {
                while (right+1<sz && arr[right+1] - arr[i]<=k) {
                    right++;
                }
                till[i] = right;
            }
            int ans = 0;
            for(int i=sz-1;i>=0;--i) {
                dp[i] = pref[till[i]] - (i>0 ? pref[i-1] : 0);
                int ispe = dp[i];
                if(i+1<sz) {
                    dp[i] = max(dp[i], dp[i + 1]);
                }
                if(till[i]+1<sz) {
                    ispe+=dp[till[i]+1];
                }
                ans = max(ans, ispe);
            }
            pl(ans);
        }
        pw.flush();
        pw.close();
    }

    static void add(Map<Integer, Integer> map, int key) {
        Integer val = map.get(key);
        if(val==null) {
            val = 0;
        }
        val++;
        map.put(key, val);
    }

    static void assert_in_range(String varName, int n, int l, int r) {
        if (n >=l && n<=r) return;
        System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
        System.exit(1);
    }
    static void assert_in_range(String varName, long n, long l, long r) {
        if (n>=l && n<=r) return;
        System.out.println(varName + " is not in range. Actual: "+n+" l : "+l+" r: "+r);
        System.exit(1);
    }

    static void initIo(boolean isFileIO, String suffix) throws IOException {
        scan = new MyScanner(isFileIO, suffix);
        if(isFileIO) {
            pw = new PrintWriter("/Users/amandeep/Desktop/output"+suffix+".txt");
        }
        else {
            pw = new PrintWriter(System.out, true);
        }
    }
    static int ni() throws IOException
    {
        return scan.nextInt();
    }
    static long nl() throws IOException
    {
        return scan.nextLong();
    }
    static double nd() throws IOException
    {
        return scan.nextDouble();
    }
    static String ne() throws IOException
    {
        return scan.next();
    }
    static String nel() throws IOException
    {
        return scan.nextLine();
    }
    static void pl()
    {
        pw.println();
    }
    static void p(Object o)
    {
        pw.print(o+" ");
    }
    static void pl(Object o)
    {
        pw.println(o);
    }
    static void psb(StringBuilder sb)
    {
        pw.print(sb);
    }
    static void pa(String arrayName, Object arr[])
    {
        pl(arrayName+" : ");
        for(Object o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, int arr[])
    {
        pl(arrayName+" : ");
        for(int o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, long arr[])
    {
        pl(arrayName+" : ");
        for(long o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, double arr[])
    {
        pl(arrayName+" : ");
        for(double o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, char arr[])
    {
        pl(arrayName+" : ");
        for(char o : arr)
            p(o);
        pl();
    }
    static void pa(String arrayName, boolean arr[])
    {
        pl(arrayName+" : ");
        for(boolean o : arr)
            p(o);
        pl();
    }
    static void pa(String listName, List list)
    {
        pl(listName+" : ");
        for(Object o : list)
            p(o);
        pl();
    }
    static void pa(String arrayName, Object[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(Object o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, int[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(int o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, long[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(long o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, char[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(char o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, double[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(double o : arr[i])
                p(o);
            pl();
        }
    }
    static void pa(String arrayName, boolean[][] arr) {
        pl(arrayName+" : ");
        for(int i=0;i<arr.length;++i) {
            for(boolean o : arr[i])
                p(o);
            pl();
        }
    }
    static class MyScanner
    {
        BufferedReader br;
        StringTokenizer st;
        MyScanner(boolean readingFromFile, String suffix) throws IOException
        {
            if(readingFromFile) {
                br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input"+suffix+".txt"));
            }
            else {
                br = new BufferedReader(new InputStreamReader(System.in));
            }
        }
        String nextLine()throws IOException
        {
            return br.readLine();
        }
        String next() throws IOException
        {
            if(st==null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }
        int nextInt() throws IOException
        {
            return Integer.parseInt(next());
        }
        long nextLong() throws IOException
        {
            return Long.parseLong(next());
        }
        double nextDouble() throws IOException
        {
            return Double.parseDouble(next());
        }
    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.util.Random;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author revanth
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        ETwoPlatforms solver = new ETwoPlatforms();
        solver.solve(1, in, out);
        out.close();
    }

    static class ETwoPlatforms {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int t = in.nextInt();
            while (t-- > 0) {
                int n = in.nextInt(), k = in.nextInt();
                int[] x = in.nextIntArray(n);
                int[] y = in.nextIntArray(n);
                in.shuffle(x);
                Arrays.sort(x);
                int[] left = new int[n];
                int[] ct = new int[n];
                for (int i = 0; i < n; i++) {
                    int l = 0, r = i, ans = i, m;
                    while (l <= r) {
                        m = (l + r) / 2;
                        if (x[i] - x[m] <= k) {
                            ans = m;
                            r = m - 1;
                        } else
                            l = m + 1;
                    }
                    left[i] = ans;
                    ct[i] = i - ans + 1;
                }
                int[] max = new int[n];
                max[0] = ct[0];
                for (int i = 1; i < n; i++)
                    max[i] = Math.max(ct[i], max[i - 1]);
                int ans = 0;
                for (int i = 0; i < n; i++) {
                    if (left[i] > 0)
                        ans = Math.max(ans, ct[i] + max[left[i] - 1]);
                    else
                        ans = Math.max(ans, ct[i]);
                }
                out.println(ans);
            }
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1 << 16];
        private int curChar;
        private int snumChars;
        private InputReader.SpaceCharFilter filter;

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

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

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

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public void shuffle(int input[]) {
            Random rand = new Random();
            for (int i = 0; i < input.length; i++) {
                int index = rand.nextInt(input.length);
                int temp = input[index];
                input[index] = input[i];
                input[i] = temp;
            }
        }

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

        public interface SpaceCharFilter {
            boolean isSpaceChar(int ch);

        }

    }

    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]);
            }
        }

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

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

    }
}


import com.sun.org.apache.xpath.internal.SourceTree;
import com.sun.org.apache.xpath.internal.functions.FuncFalse;
import javafx.util.Pair;

import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Contest {
    public static class pair implements Comparable<pair> {
        long l;
        long r;

        public pair(long lef, long ri) {
            l = lef;
            r = ri;
        }

        @Override
        public int compareTo(pair p) {
            if (l == p.l)
                return Long.compare(r, p.r);
            return Long.compare(l, p.l);
        }
    }

    static int n, m;
    static ArrayList<Integer>[] adj;
    static int dist[];
    static int[] h;
    static int[] p;
    static int parent[];
    static long inf = (long) 1e18;
    static int INF = (int) 1e9;

    public static boolean isSimilar(char[] Arr, int s, int k) {
        for (int i = s; i < k - 1; i++) {
            if (Arr[i + 1] != Arr[i])
                return false;
        }
        return true;
    }

    public static int ciel(int d, int mod) {
        if (d % mod == 0)
            return d / mod;
        return (d / mod) + 1;
    }

    public static long gcd(long u, long v) {
        if (v == 0)
            return u;
        return gcd(v, u % v);
    }

    static int k, Arr[], lefAcc[], riAcc[];
    static PrintWriter pw;

    static long memo[][];
    static char[][] Matrix;
    static int[] dx = new int[]{-1, 1, 0, 0};

    static int[] dy = new int[]{0, 0, -1, 1};

    static boolean valid(int i, int j) {

        return i != -1 && j != -1 && i != n && j != m;

    }

//    static boolean[][] vis = new boolean[n][m];
//
//    static void dfs2(int i, int j) {
//
//        vis[i][j] = true;                    //mark as visited
//
//        for (int k = 0; k < 4; ++k) {
//
//            int x = i + dx[k], y = j + dy[k];
//
//            if (valid(x, y) && Matrix[x][y] != '#' && !vis[x][y])
//
//                dfs2(x, y);
//
//        }
//
//    }

    public static int getnum(int[][] matrix, int i, int j) {
        int c = 0;

        for (int k = 0; k < 4; ++k) {

            int x = i + dx[k], y = j + dy[k];

            if (valid(x, y))
                c++;

        }
        return c;
    }

    static String str;

    public static int howManyChar(char c, int lo, int hi) {
        int cnt = 0;
        for (int i = lo; i <= hi; i++) {
            if (str.charAt(i) == c) cnt++;
        }
        return (hi - lo + 1) - cnt;
    }

    public static long divCon(int lo, int hi, char c) {
        if (lo == hi)
            return c == str.charAt(lo) ? 0 : 1;
        int mid = (lo + hi) >> 1;
        char next = (char) (c + 1);
        return min(howManyChar(c, lo, mid) + divCon(mid + 1, hi, next),
                howManyChar(c, mid + 1, hi) + divCon(lo, mid, next));
    }

    static Stack<Integer> topo;

    public static int dfs(int u) {
        vis[u] = true;
        int ans = 0;
        for (int v : adj[u]) {
            if (!vis[v])
                ans += dfs(v);
        }
        return dist[u] = ans + p[u];
    }

    static boolean ans;

    public static int dfs2(int u) {
        vis[u] = true;
        ans &= abs(h[u]) <= dist[u] && ((dist[u] + h[u]) % 2 == 0);
        int pos = ((dist[u] + h[u]) / 2);
        int neg = dist[u] - pos;
        int dif = neg - p[u];
        if (dif > 0) dif = 0;
        int res = 0;
        for (int v : adj[u])
            if (!vis[v])
                res += dfs2(v);

        ans &= pos + dif >= res;
        return pos;
    }

    static boolean vis[];

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        pw = new PrintWriter(System.out);

        int t = sc.nextInt();
        while (t-- > 0) {
            n = sc.nextInt();
            int k=sc.nextInt();
            int[]Arr=new int[n];
            for (int i = 0; i < n; i++) {
                Arr[i]=sc.nextInt();
            }
            for (int i = 0; i < n; i++) {
               sc.nextInt();
            }
            Arrays.sort(Arr);
            int []dp=new int[n];
            for (int i = 0; i < n; i++) {
                int lo=i;
                int hi=n-1;
                int mid;
                int ans=i;
                while(lo<=hi){
                    mid=(lo+hi)/2;
                    if(Arr[mid]<=Arr[i]+k){
                        ans=mid;
                        lo=mid+1;
                    }else
                        hi=mid-1;
                }
                dp[i]=ans-i+1;
            }
            int[]max=new int[n+1];
            for (int i = n-1; i >=0; i--) {
                max[i]=max(dp[i],max[i+1]);
            }
            int ans=0;
            for (int i = 0; i < n; i++) {
                ans=max(ans,dp[i]+max[dp[i]+i]);
            }
            pw.println(ans);
        }
        pw.close();

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException, IOException {
            return br.ready();
        }

    }
}


import com.sun.org.apache.xpath.internal.SourceTree;
import com.sun.org.apache.xpath.internal.functions.FuncFalse;
import javafx.util.Pair;

import java.io.*;
import java.util.*;

import static java.lang.Math.*;

public class Contest {
    public static class pair implements Comparable<pair> {
        long l;
        long r;

        public pair(long lef, long ri) {
            l = lef;
            r = ri;
        }

        @Override
        public int compareTo(pair p) {
            if (l == p.l)
                return Long.compare(r, p.r);
            return Long.compare(l, p.l);
        }
    }

    static int n, m;
    static ArrayList<Integer>[] adj;
    static int dist[];
    static int[] h;
    static int[] p;
    static int parent[];
    static long inf = (long) 1e18;
    static int INF = (int) 1e9;

    public static boolean isSimilar(char[] Arr, int s, int k) {
        for (int i = s; i < k - 1; i++) {
            if (Arr[i + 1] != Arr[i])
                return false;
        }
        return true;
    }

    public static int ciel(int d, int mod) {
        if (d % mod == 0)
            return d / mod;
        return (d / mod) + 1;
    }

    public static long gcd(long u, long v) {
        if (v == 0)
            return u;
        return gcd(v, u % v);
    }

    static int k, Arr[], lefAcc[], riAcc[];
    static PrintWriter pw;

    static long memo[][];
    static char[][] Matrix;
    static int[] dx = new int[]{-1, 1, 0, 0};

    static int[] dy = new int[]{0, 0, -1, 1};

    static boolean valid(int i, int j) {

        return i != -1 && j != -1 && i != n && j != m;

    }

//    static boolean[][] vis = new boolean[n][m];
//
//    static void dfs2(int i, int j) {
//
//        vis[i][j] = true;                    //mark as visited
//
//        for (int k = 0; k < 4; ++k) {
//
//            int x = i + dx[k], y = j + dy[k];
//
//            if (valid(x, y) && Matrix[x][y] != '#' && !vis[x][y])
//
//                dfs2(x, y);
//
//        }
//
//    }

    public static int getnum(int[][] matrix, int i, int j) {
        int c = 0;

        for (int k = 0; k < 4; ++k) {

            int x = i + dx[k], y = j + dy[k];

            if (valid(x, y))
                c++;

        }
        return c;
    }

    static String str;

    public static int howManyChar(char c, int lo, int hi) {
        int cnt = 0;
        for (int i = lo; i <= hi; i++) {
            if (str.charAt(i) == c) cnt++;
        }
        return (hi - lo + 1) - cnt;
    }

    public static long divCon(int lo, int hi, char c) {
        if (lo == hi)
            return c == str.charAt(lo) ? 0 : 1;
        int mid = (lo + hi) >> 1;
        char next = (char) (c + 1);
        return min(howManyChar(c, lo, mid) + divCon(mid + 1, hi, next),
                howManyChar(c, mid + 1, hi) + divCon(lo, mid, next));
    }

    static Stack<Integer> topo;

    public static int dfs(int u) {
        vis[u] = true;
        int ans = 0;
        for (int v : adj[u]) {
            if (!vis[v])
                ans += dfs(v);
        }
        return dist[u] = ans + p[u];
    }

    static boolean ans;

    public static int dfs2(int u) {
        vis[u] = true;
        ans &= abs(h[u]) <= dist[u] && ((dist[u] + h[u]) % 2 == 0);
        int pos = ((dist[u] + h[u]) / 2);
        int neg = dist[u] - pos;
        int dif = neg - p[u];
        if (dif > 0) dif = 0;
        int res = 0;
        for (int v : adj[u])
            if (!vis[v])
                res += dfs2(v);

        ans &= pos + dif >= res;
        return pos;
    }

    static boolean vis[];

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        pw = new PrintWriter(System.out);

        int t = sc.nextInt();
        while (t-- > 0) {
            n = sc.nextInt();
            int k=sc.nextInt();
            Integer[]Arr=new Integer[n];
            for (int i = 0; i < n; i++) {
                Arr[i]=sc.nextInt();
            }
            for (int i = 0; i < n; i++) {
               sc.nextInt();
            }
            Arrays.sort(Arr);
            int []dp=new int[n];
            for (int i = 0; i < n; i++) {
                int lo=i;
                int hi=n-1;
                int mid;
                int ans=i;
                while(lo<=hi){
                    mid=(lo+hi)/2;
                    if(Arr[mid]<=Arr[i]+k){
                        ans=mid;
                        lo=mid+1;
                    }else
                        hi=mid-1;
                }
                dp[i]=ans-i+1;
            }
            int[]max=new int[n+1];
            for (int i = n-1; i >=0; i--) {
                max[i]=max(dp[i],max[i+1]);
            }
            int ans=0;
            for (int i = 0; i < n; i++) {
                ans=max(ans,dp[i]+max[dp[i]+i]);
            }
            pw.println(ans);
        }
        pw.close();

    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s) {
            br = new BufferedReader(new InputStreamReader(s));
        }

        public String next() throws IOException {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

        public int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        public long nextLong() throws IOException {
            return Long.parseLong(next());
        }

        public String nextLine() throws IOException {
            return br.readLine();
        }

        public double nextDouble() throws IOException {
            String x = next();
            StringBuilder sb = new StringBuilder("0");
            double res = 0, f = 1;
            boolean dec = false, neg = false;
            int start = 0;
            if (x.charAt(0) == '-') {
                neg = true;
                start++;
            }
            for (int i = start; i < x.length(); i++)
                if (x.charAt(i) == '.') {
                    res = Long.parseLong(sb.toString());
                    sb = new StringBuilder("0");
                    dec = true;
                } else {
                    sb.append(x.charAt(i));
                    if (dec)
                        f *= 10;
                }
            res += Long.parseLong(sb.toString()) / f;
            return res * (neg ? -1 : 1);
        }

        public boolean ready() throws IOException, IOException {
            return br.ready();
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pranay2516
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ETwoPlatforms solver = new ETwoPlatforms();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class ETwoPlatforms {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), x[] = in.readIntArray(n), y[] = in.readIntArray(n), l[] = new int[n], r[] = new int[n], j = 0, ans = 1;
            for (int i = 0; i < n; ++i) {
                while (x[i] - x[j] > k) ++j;
                l[i] = i - j + 1;
                if (i > 0) l[i] = Math.max(l[i], l[i - 1]);
            }
            j = n - 1;
            for (int i = n - 1; i >= 0; --i) {
                while (x[j] - x[i] > k) --j;
                r[i] = j - i + 1;
                if (i < n - 1) r[i] = Math.max(r[i], r[i + 1]);
            }
            for (int i = 0; i < n - 1; ++i) ans = Math.max(ans, r[i + 1] + l[i]);
            out.println(ans);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(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 nextInt() {
            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 next() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int[] readIntArray(int size) {
            int[] array = new int[(int) size];
            for (int i = 0; i < size; i++) array[i] = nextInt();
            return array;
        }

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

        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pranay2516
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ETwoPlatforms solver = new ETwoPlatforms();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class ETwoPlatforms {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), x[] = in.readIntArray(n), y[] = in.readIntArray(n), l[] = new int[n], r[] = new int[n], j = 0, ans = 1;
            func.sort(x);
            for (int i = 0; i < n; ++i) {
                while (x[i] - x[j] > k) ++j;
                l[i] = i - j + 1;
                if (i > 0) l[i] = Math.max(l[i], l[i - 1]);
            }
            j = n - 1;
            for (int i = n - 1; i >= 0; --i) {
                while (x[j] - x[i] > k) --j;
                r[i] = j - i + 1;
                if (i < n - 1) r[i] = Math.max(r[i], r[i + 1]);
            }
            for (int i = 0; i < n - 1; ++i) ans = Math.max(ans, r[i + 1] + l[i]);
            out.println(ans);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(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 nextInt() {
            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 next() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int[] readIntArray(int size) {
            int[] array = new int[(int) size];
            for (int i = 0; i < size; i++) array[i] = nextInt();
            return array;
        }

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

        }

    }

    static class func {
        public static void sort(int[] arr) {
            //Taken from @KharYusuf
            int n = arr.length, mid, h, s, l, i, j, k;
            int[] res = new int[n];
            n--;
            for (s = 1; s <= n; s <<= 1) {
                for (l = 0; l < n; l += (s << 1)) {
                    h = Math.min(l + (s << 1) - 1, n);
                    mid = Math.min(l + s - 1, n);
                    i = l;
                    j = mid + 1;
                    k = l;
                    while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]);
                    while (i <= mid) res[k++] = arr[i++];
                    while (j <= h) res[k++] = arr[j++];
                    for (k = l; k <= h; k++) arr[k] = res[k];
                }
            }
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pranay2516
 */
public class Main {

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		FastReader in = new FastReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		ETwoPlatforms solver = new ETwoPlatforms();
		int testCount = Integer.parseInt(in.next());
		for (int i = 1; i <= testCount; i++)
			solver.solve(i, in, out);
		out.close();
	}

	static class ETwoPlatforms {

		public void solve(int testNumber, FastReader in, PrintWriter out) {
			int n = in.nextInt(), k = in.nextInt(), x[] = in.readIntArray(n), y[] = in
					.readIntArray(n), l[] = new int[n], r[] = new int[n], j = 0, ans = 1;
			func.sort(x);
			for (int i = 0; i < n; ++i) {
				while (x[i] - x[j] > k)
					++j;
				l[i] = i - j + 1;
				if (i > 0)
					l[i] = Math.max(l[i], l[i - 1]);
			}
			j = n - 1;
			for (int i = n - 1; i >= 0; --i) {
				while (x[j] - x[i] > k)
					--j;
				r[i] = j - i + 1;
				if (i < n - 1)
					r[i] = Math.max(r[i], r[i + 1]);
			}
			for (int i = 0; i < n - 1; ++i)
				ans = Math.max(ans, r[i + 1] + l[i]);
			out.println(ans);
		}

	}

	static class func {

		public static void sort(int[] arr) {
			//Taken from @KharYusuf
			int n = arr.length, mid, h, s, l, i, j, k;
			int[] res = new int[n];
			n--;
			for (s = 1; s <= n; s <<= 1) {
				for (l = 0; l < n; l += (s << 1)) {
					h = Math.min(l + (s << 1) - 1, n);
					mid = Math.min(l + s - 1, n);
					i = l;
					j = mid + 1;
					k = l;
					while (i <= mid && j <= h)
						res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]);
					while (i <= mid)
						res[k++] = arr[i++];
					while (j <= h)
						res[k++] = arr[j++];
					for (k = l; k <= h; k++)
						arr[k] = res[k];
				}
			}
		}

	}

	static class FastReader {

		private InputStream stream;

		private byte[] buf = new byte[1024];

		private int curChar;

		private int numChars;

		private FastReader.SpaceCharFilter filter;

		public FastReader(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 nextInt() {
			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 next() {
			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 boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public int[] readIntArray(int size) {
			int[] array = new int[(int) size];
			for (int i = 0; i < size; i++)
				array[i] = nextInt();
			return array;
		}

		public interface SpaceCharFilter {

			public boolean isSpaceChar(int ch);

		}

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Pranay2516
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ETwoPlatforms solver = new ETwoPlatforms();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class ETwoPlatforms {
        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt(), x[] = in.readIntArray(n), y[] = in.readIntArray(n), l[] = new int[n], r[] = new int[n], j = 0, ans = 1;
            Arrays.sort(x);
            for (int i = 0; i < n; ++i) {
                while (x[i] - x[j] > k) ++j;
                l[i] = i - j + 1;
                if (i > 0) l[i] = Math.max(l[i], l[i - 1]);
            }
            j = n - 1;
            for (int i = n - 1; i >= 0; --i) {
                while (x[j] - x[i] > k) --j;
                r[i] = j - i + 1;
                if (i < n - 1) r[i] = Math.max(r[i], r[i + 1]);
            }
            for (int i = 0; i < n - 1; ++i) ans = Math.max(ans, r[i + 1] + l[i]);
            out.println(ans);
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(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 nextInt() {
            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 next() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public int[] readIntArray(int size) {
            int[] array = new int[(int) size];
            for (int i = 0; i < size; i++) array[i] = nextInt();
            return array;
        }

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

        }

    }
}