#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; ll K; cin >> N >> K; vector A(N), B(N), C(N), D(N); for (int i = 0; i < N; i++) cin >> A[i]; for (int i = 0; i < N; i++) cin >> B[i]; for (int i = 0; i < N; i++) cin >> C[i]; for (int i = 0; i < N; i++) cin >> D[i]; // multiset: (cost, count) map mp; ll total = 0; ll gold = 0; for (int i = 0; i < N; i++) { // add buys mp[A[i]] += B[i]; total += B[i]; // keep only best K (remove largest cost) while (total > K) { auto it = prev(mp.end()); // largest cost ll cost = it->first; ll cnt = it->second; ll remove = min(cnt, total - K); mp[cost] -= remove; total -= remove; if (mp[cost] == 0) mp.erase(cost); } // sell ll need = D[i]; while (need > 0 && !mp.empty()) { auto it = mp.begin(); // smallest cost ll cost = it->first; ll cnt = it->second; ll take = min(cnt, need); gold += take * (C[i] - cost); mp[cost] -= take; total -= take; need -= take; if (mp[cost] == 0) mp.erase(cost); } } cout << gold << '\n'; } }