#include <bits/stdc++.h>
using namespace std;
struct point {
  int x, y, l, r;
};
bool cmp(point p1, point p2) { return p1.x < p2.x; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long int n, k;
    cin >> n >> k;
    point a[n];
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      a[i].x = x;
    }
    for (int i = 0; i < n; i++) {
      int y;
      cin >> y;
      a[i].y = y;
    }
    sort(a, a + n, cmp);
    for (int i = 0; i < n; i++) a[i].l = i + 1, a[i].r = n - i;
    int l = n - 2, r = n - 1;
    while (l >= 0) {
      if (a[l].x == a[r].x)
        l--;
      else if (a[l].x >= a[r].x - k) {
        l--;
      } else
        a[r].l -= a[l].l, r--;
    }
    l = 0, r = 1;
    while (r < n) {
      if (a[l].x == a[r].x)
        r++;
      else if (a[l].x + k >= a[r].x) {
        r++;
      } else
        a[l].r -= a[r].r, l++;
    }
    for (int i = 1; i < n; i++) a[i].l = max(a[i].l, a[i - 1].l);
    for (int i = n - 2; i >= 0; i--) a[i].r = max(a[i].r, a[i + 1].r);
    int ans = 1;
    for (int i = 0; i < n - 1; i++) ans = max(ans, a[i].l + a[i + 1].r);
    cout << ans << endl;
  }
}
