#include using namespace std; random_device rnd; mt19937 mt(rnd()); int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--){ long long N,K; cin >> N >> K; vector A(N),B(N),C(N),D(N); for(auto &a : A) cin >> a; for(auto &a : B) cin >> a; for(auto &a : C) cin >> a; for(auto &a : D) cin >> a; map M; long long hold = 0,answer = 0; for(int i=0; ifirst < c){ auto &[v,w] = *M.begin(); long long buy = min(d,w); answer += (c-v)*buy; M[c] += buy,w -= buy,d -= buy; if(w == 0) M.erase(M.begin()); } while(hold > K){ auto &[v,w] = *M.rbegin(); long long dec = min(w,hold-K); hold -= dec,w -= dec; if(w == 0) M.erase(--M.end()); } } cout << answer << "\n"; } }