#include <bits/stdc++.h>
using namespace std;
const int Nmax = 5e5 + 112;
int q, n, m, c, c0, T;
long long ans = 0;
bool check = false;
struct person {
  int t, a, b;
  bool operator<(person other) const { return t < other.t; }
} p[Nmax];
multiset<pair<int, int> > s;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> q;
  while (q--) {
    cin >> n >> m >> c >> c0;
    for (int i = (int)1; i <= (int)n; ++i) {
      cin >> p[i].t >> p[i].a >> p[i].b;
    }
    s.clear();
    s.insert({0, c0});
    check = false;
    p[0] = {0, 0, 0};
    ans = 0;
    T = c0;
    sort(p + 1, p + n + 1);
    p[n + 1] = {m, 0, 0};
    for (int i = (int)1; i <= (int)n + 1; ++i) {
      int dis = p[i].t - p[i - 1].t;
      if (dis > c) {
        check = true;
        break;
      }
      while (s.size() && dis) {
        pair<int, int> to = *s.begin();
        s.erase(s.begin());
        T -= to.second;
        if (dis > to.second) {
          dis -= to.second;
          ans += 1LL * to.first * to.second;
        } else {
          to.second -= dis;
          ans += 1LL * dis * to.first;
          dis = 0;
          if (to.second == 0)
            break;
          else {
            T += to.second;
            s.insert(to);
          }
        }
      }
      if (dis > 0) {
        check = true;
        break;
      }
      if (i <= n) {
        T += p[i].a;
        s.insert(pair<int, int>(p[i].b, p[i].a));
      }
      while (s.size() && T > c) {
        pair<int, int> tmp = *s.rbegin();
        s.erase(s.find(tmp));
        if (T - c > tmp.second)
          T -= tmp.second;
        else {
          tmp.second -= T - c;
          T = c;
          if (tmp.second > 0) s.insert(tmp);
        }
      }
    }
    if (check == true)
      cout << -1 << '\n';
    else
      cout << ans << '\n';
  }
}
