#include <bits/stdc++.h>
using namespace std;
multiset<pair<int, int>> P;
vector<tuple<int, int, int>> F;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ((void)0);
  ((void)0);
  ((void)0);
  int T, N, M, C0, C, ct;
  for (cin >> T; T--;) {
    long long ans = ct = 0;
    cin >> N >> M >> C >> C0;
    F.resize(N);
    for (auto &[t, a, b] : F) cin >> t >> a >> b;
    sort(F.begin(), F.end());
    F.emplace_back(M, 0, 0);
    P.insert({0, C0});
    for (auto &[t, a, b] : F) {
      int diff = t - ct;
      ct = t;
      if ((C0 -= diff) < 0) {
        ans = -1;
        break;
      }
      while (diff && P.size()) {
        auto temp = *P.begin();
        int m = min(diff, temp.second);
        P.erase(P.begin());
        diff -= m;
        ans += 1LL * m * temp.first;
        if (temp.second -= m) P.insert(temp);
      }
      while (C0 + a > C && P.size() && P.rbegin()->first > b) {
        auto temp = *P.rbegin();
        int m = min(C0 + a - C, temp.second);
        P.erase(--P.end());
        temp.second -= m;
        C0 -= m;
        if (temp.second) P.insert(temp);
      }
      diff = min(C0 + a, C) - C0;
      C0 += diff;
      P.insert({b, diff});
    }
    P.clear();
    cout << ans << '\n';
  }
  return 0;
}
