#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
int a[MAXN], b[MAXN], r[MAXN];
int t, n, k;
bool cmp(int a, int b) { return a < b; }
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i <= n + 1; i++) r[i] = 0;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    sort(a + 1, a + 1 + n, cmp);
    for (int i = n; i >= 1; i--) {
      int now = a[i] + k;
      int net = upper_bound(a + 1, a + 1 + n, now) - a;
      r[i] = max(r[i + 1], net - i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int now = a[i] + k;
      int net = upper_bound(a + 1, a + 1 + n, now) - a;
      ans = max(ans, r[net] + (net - i));
    }
    cout << ans << endl;
  }
  return 0;
}
