#include <bits/stdc++.h>
using namespace std;
const int MX = 6e5;
int q;
long long n, m, cmax, c;
long long t[MX], a[MX], b[MX], T[MX], A[MX], B[MX], SA[MX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> q;
  while (q--) {
    cin >> n >> m >> cmax >> c;
    for (int i = int(0); i < int(n); i++)
      cin >> T[i] >> A[i] >> B[i], SA[i] = i;
    sort(SA, SA + n, [](long long a, long long b) { return T[a] < T[b]; });
    for (int i = int(0); i < int(n); i++)
      t[i] = T[SA[i]], a[i] = A[SA[i]], b[i] = B[SA[i]];
    t[n] = m, a[n] = 0, b[n] = 0;
    long long payed = 0, i = 0;
    map<int, int> pq;
    pq[0] += c;
    int sum = c;
    while (i <= n) {
      int moved = t[i] - (i == 0 ? 0 : t[i - 1]);
      sum -= moved;
      while (moved != 0 && !pq.empty()) {
        auto it = pq.begin();
        if (it->second > moved) {
          payed += (long long)it->first * (long long)moved;
          it->second -= moved;
          moved = 0;
        } else {
          payed += (long long)it->first * (long long)it->second;
          moved -= it->second;
          pq.erase(it);
        }
      }
      if (moved != 0) {
        payed = -1;
        break;
      }
      pq[b[i]] += a[i];
      sum += a[i];
      while (sum > cmax) {
        auto it = --pq.end();
        if (sum - it->second < cmax) {
          it->second -= sum - cmax;
          sum = cmax;
        } else {
          sum -= it->second;
          pq.erase(it);
        }
      }
      i++;
    }
    cout << payed << endl;
  }
}
