#include <bits/stdc++.h>
using namespace std;
class myfriend {
 public:
  long long t, a, b;
};
bool cmp(myfriend& a, myfriend& b) { return a.t < b.t; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, m, c, c0;
    cin >> n >> m >> c >> c0;
    vector<myfriend> v(n + 1);
    v[0].t = 0;
    v[0].a = c0;
    v[0].b = 0;
    for (int i = 1; i <= n; i++) cin >> v[i].t >> v[i].a >> v[i].b;
    sort(v.begin(), v.end(), cmp);
    multiset<pair<long long, long long> > s;
    long long total = 0, ans = 0;
    for (int i = 0; i <= n; i++) {
      s.insert({v[i].b, v[i].a});
      total += v[i].a;
      while (total > c) {
        pair<long long, long long> e = *prev(s.end());
        s.erase(prev(s.end()));
        total -= e.second;
        if (total < c) {
          e.second = c - total;
          total = c;
          s.insert(e);
        }
      }
      long long time = (i == n ? m - v[i].t : v[i + 1].t - v[i].t);
      if (time > total) {
        ans = -1;
        break;
      }
      while (time) {
        pair<long long, long long> b = *s.begin();
        s.erase(s.begin());
        time -= b.second;
        total -= b.second;
        ans += b.first * b.second;
        if (time < 0) {
          b.second = -time;
          total += -time;
          ans += b.first * time;
          time = 0;
          s.insert(b);
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
