#include using namespace std; using int64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; int64 K; cin >> N >> K; vector A(N), B(N), C(N), D(N); for (auto &x : A) cin >> x; for (auto &x : B) cin >> x; for (auto &x : C) cin >> x; for (auto &x : D) cin >> x; map slopes; int64 slope_count = 0; int64 answer = 0; auto add_slope = [&](int64 slope, int64 count) { if (count <= 0) return; slopes[slope] += count; slope_count += count; }; auto erase_from_iterator = [&](map::iterator it, int64 count) { it->second -= count; slope_count -= count; if (it->second == 0) slopes.erase(it); }; for (int i = 0; i < N; i++) { add_slope(-A[i], B[i]); int64 remaining = D[i]; while (remaining > 0 && !slopes.empty()) { auto it = prev(slopes.end()); int64 best = it->first; if (best + C[i] <= 0) break; int64 take = min(remaining, it->second); answer += take * (best + C[i]); erase_from_iterator(it, take); add_slope(-C[i], take); remaining -= take; } while (slope_count > K) { auto it = slopes.begin(); int64 drop = min(slope_count - K, it->second); erase_from_iterator(it, drop); } } cout << answer << '\n'; } return 0; }