#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
struct Man {
  int t, a, b;
  bool operator<(const Man &m) const { return t < m.t; }
};
int N, M, C0, C;
map<int, int> dp;
Man ms[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    dp.clear();
    cin >> N >> M >> C >> C0;
    for (int i = 0; i < N; i++) {
      int t, a, b;
      cin >> t >> a >> b;
      ms[i] = {t, a, b};
    }
    ms[N++] = {0, C0, 0};
    ms[N++] = {M, 0, 0};
    long long ans = 0;
    sort(ms, ms + N);
    int tot = 0;
    for (int i = 0; i < N; i++) {
      if (i > 0 && ms[i].t != ms[i - 1].t) {
        int diff = ms[i].t - ms[i - 1].t;
        while (!dp.empty() && diff > 0) {
          auto it = dp.begin();
          if (it->second <= diff) {
            diff -= it->second;
            tot -= it->second;
            ans += (long long)it->first * it->second;
            dp.erase(it);
          } else {
            tot -= diff;
            ans += (long long)it->first * diff;
            it->second -= diff;
            diff = 0;
          }
        }
        if (diff > 0) {
          ans = -1;
          break;
        }
      }
      tot += ms[i].a;
      dp[ms[i].b] += ms[i].a;
      while (tot > C) {
        auto it = --dp.end();
        if (it->second <= tot - C) {
          tot -= it->second;
          dp.erase(it);
        } else {
          it->second -= tot - C;
          tot = C;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
