#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 10;
long long a[maxn], b[maxn], c[maxn], pre[maxn];
bool com(long long a, long long b) { return a < b; }
long long qian[maxn], hou[maxn];
signed main() {
  long long t, n, k;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    for (long long i = 1; i <= n; i++) cin >> b[i];
    sort(a + 1, a + 1 + n, com);
    long long top = 0, last = a[1], shu = 1;
    for (long long i = 2; i <= n; i++) {
      if (a[i] == a[i - 1])
        shu++;
      else {
        b[++top] = last, c[top] = shu;
        last = a[i], shu = 1;
      }
    }
    b[++top] = a[n], c[top] = shu;
    for (long long i = 1; i <= top; i++) pre[i] = pre[i - 1] + c[i];
    for (long long i = 1; i <= top; i++) {
      long long nowx = b[i] + k;
      long long index = upper_bound(b + 1, b + 1 + top, nowx) - b;
      index--;
      qian[index] = max(qian[index], pre[index] - pre[i - 1]);
    }
    for (long long i = 1; i <= top; i++) qian[i] = max(qian[i - 1], qian[i]);
    for (long long i = top; i >= 1; i--) {
      long long nowx = b[i] + k;
      long long index = upper_bound(b + 1, b + 1 + top, nowx) - b;
      index--;
      hou[i] = max(hou[i + 1], pre[index] - pre[i - 1]);
    }
    long long ans = 0;
    for (long long i = 0; i <= top; i++) ans = max(ans, qian[i] + hou[i + 1]);
    cout << ans << endl;
    for (long long i = 0; i <= n + 1; i++)
      a[i] = b[i] = c[i] = pre[i] = qian[i] = hou[i] = 0;
  }
}
