#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve() {
  ll N, M, C, C0;
  cin >> N >> M >> C >> C0;
  vector<int> ord(N);
  for (int i = 0; i < (N); i++) ord[i] = i;
  vector<ll> T(N), A(N), B(N);
  for (int i = 0; i < (N); i++) cin >> T[i] >> A[i] >> B[i];
  ord.push_back(N);
  T.push_back(M);
  A.push_back(0);
  B.push_back(0);
  sort(ord.begin(), ord.end(), [&](int i, int j) { return T[i] < T[j]; });
  map<ll, ll> mp;
  mp[0] = C0;
  ll cur = C0;
  ll ans = 0;
  ll pre = 0;
  for (int i : ord) {
    ll dif = T[i] - pre;
    while (dif > 0 && !mp.empty()) {
      auto it = mp.begin();
      ll dec = min(dif, it->second);
      it->second -= dec;
      dif -= dec;
      cur -= dec;
      if (it->second == 0) {
        mp.erase(it);
      }
    }
    if (dif > 0) return -1;
    cur += A[i];
    mp[B[i]] += A[i];
    ans += A[i] * B[i];
    while (cur > C) {
      auto it = prev(mp.end());
      ll dec = min(cur - C, it->second);
      it->second -= dec;
      ans -= dec * it->first;
      cur -= dec;
      if (it->second == 0) {
        mp.erase(it);
      }
    }
    pre = T[i];
  }
  for (auto kv : mp) ans -= kv.first * kv.second;
  return ans;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int Q;
  cin >> Q;
  while (Q--) {
    cout << solve() << '\n';
  }
}
