#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> w(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
// 計算後綴和
vector<int> s(n);
s[n - 1] = w[n - 1];
for (int i = n - 2; i >= 0; i--) {
s[i] = s[i + 1] + w[i];
}
int ans = 0;
int m = 0;
for (int i = 0; i < n; i++) {
// 使用二分搜尋找到符合條件的區間右界
int left = max(i + 1, m), right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (a[mid] - a[i] > k) {
right = mid - 1;
} else {
left = mid + 1;
}
}
if (left <= n - 1) {
ans += w[i] * s[left];
}
m = left;
}
cout << ans << '\n';
}
return 0;
}