#include using namespace std; using int64 = long long; using i128 = __int128_t; static void print_i128(i128 x) { if (x == 0) { cout << 0 << '\n'; return; } string s; while (x > 0) { s.push_back(char('0' + (int)(x % 10))); x /= 10; } reverse(s.begin(), s.end()); cout << s << '\n'; } 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 (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]; // key = sell price, value = count of unmatched future sell slots map mp; int64 sz = 0; i128 ans = 0; for (int i = N - 1; i >= 0; i--) { // 1) Add today's sell slots mp[C[i]] += D[i]; sz += D[i]; // 2) Use today's buys to match highest profitable sell slots int64 rem = B[i]; while (rem > 0 && !mp.empty()) { auto it = prev(mp.end()); // largest sell price int64 price = it->first; int64 cnt = it->second; if (price <= A[i]) break; // no positive profit anymore int64 take = min(rem, cnt); ans += (i128)take * (price - A[i]); rem -= take; sz -= take; it->second -= take; if (it->second == 0) mp.erase(it); } // 3) At most K sell slots can be carried further to the past while (sz > K) { auto it = mp.begin(); // discard smallest sell prices int64 cnt = it->second; int64 drop = min(cnt, sz - K); it->second -= drop; sz -= drop; if (it->second == 0) mp.erase(it); } } print_i128(ans); } return 0; }