#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    int n, m, c, c0;
    cin >> n >> m >> c >> c0;
    map<int, int> M;
    M[0] = c0;
    vector<pair<int, pair<int, int> > > event;
    for (int i = 1, t, a, b; i <= n; i++) {
      cin >> t >> a >> b;
      event.emplace_back(make_pair(t, make_pair(a, b)));
    }
    sort(event.begin(), event.end());
    event.emplace_back(make_pair(m, make_pair(0, 0)));
    int pre = 0;
    int sum = c0;
    long long int ans = 0;
    for (int i = 0; i <= n; i++) {
      int now = event[i].first, diff = now - pre, a = event[i].second.first,
          b = event[i].second.second;
      swap(a, b);
      if (diff > sum) {
        ans = -1;
        break;
      }
      while (diff > 0) {
        auto it = M.begin();
        if (it->second > diff) {
          sum -= diff;
          ans += (long long int)diff * it->first;
          it->second -= diff;
          diff = 0;
        } else {
          sum -= it->second;
          ans += (long long int)it->second * it->first;
          diff -= it->second;
          M.erase(it);
        }
      }
      if (now == m) break;
      if (M.size() == 0) {
        M[a] = min(b, c);
        sum += M[a];
      } else {
        int nxtsum = min(c, sum + b);
        b = b - (nxtsum - sum);
        while (b > 0) {
          auto it = --M.end();
          if (a < it->first) {
            if (b < it->second) {
              it->second -= b;
              sum -= b;
              b -= b;
            } else {
              sum -= it->second;
              b -= it->second;
              M.erase(it);
            }
          } else
            break;
        }
        M[a] += (nxtsum - sum);
        sum = nxtsum;
      }
      pre = now;
    }
    cout << ans << '\n';
  }
}
