#include using namespace std; typedef long long ll; typedef __int128 lll; void solve() { 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]; map mp; ll total = 0; lll profit = 0; auto add = [&](ll price, ll count) { mp[price] += count; total += count; }; for (int i = 0; i < N; i++) { ll a = A[i], b = B[i], c = C[i], d = D[i]; add(a, b); ll to_sell = d, sold = 0; while (to_sell > 0 && !mp.empty()) { auto it = mp.begin(); if (it->first >= c) break; ll price = it->first; ll count = it->second; ll take = min(count, to_sell); profit += (lll)(c - price) * take; total -= take; to_sell -= take; sold += take; if (take == count) mp.erase(it); else it->second -= take; } if (sold > 0) add(c, sold); while (total > K) { auto it = prev(mp.end()); ll remove = min(it->second, total - K); total -= remove; if (remove == it->second) mp.erase(it); else it->second -= remove; } } if (profit == 0) { cout << 0 << "\n"; return; } string s; lll p = profit; while (p > 0) { s += char('0' + (int)(p % 10)); p /= 10; } reverse(s.begin(), s.end()); cout << s << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) solve(); return 0; }