#include <bits/stdc++.h>
template <class T>
inline bool cmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
inline bool cmin(T& a, const T& b) {
  return a > b ? a = b, 1 : 0;
}
using namespace std;
char ss[1 << 17], *A = ss, *B = ss;
inline char gc() {
  return A == B && (B = (A = ss) + fread(ss, 1, 1 << 17, stdin), A == B) ? -1
                                                                         : *A++;
}
template <class T>
inline void sd(T& x) {
  char c;
  T y = 1;
  while (c = gc(), (c < 48 || 57 < c) && c != -1)
    if (c == 45) y = -1;
  x = c - 48;
  while (c = gc(), 47 < c && c < 58) x = x * 10 + c - 48;
  x *= y;
}
const int N = 5e5 + 5, M = N << 1;
typedef int arr[N];
struct Water {
  int t, a, b;
  inline void in() { sd(t), sd(a), sd(b); }
  bool operator<(const Water& y) const {
    return t == y.t ? (a == y.a ? b < y.b : a < y.a) : t < y.t;
  }
} a[N];
int n, m, c, res;
map<int, int> q;
inline long long sol() {
  long long ans = 0;
  sd(n), sd(m), sd(c), sd(res);
  for (register int i = 1, I = n + 1; i < I; ++i) a[i].in();
  a[++n] = (Water){m, 0, 0};
  sort(a + 1, a + n + 1);
  q.clear();
  q[0] = res;
  for (register int i = 1, I = n + 1; i < I; ++i) {
    int DeltaT = a[i].t - a[i - 1].t;
    if (DeltaT > c) return -1;
    while (!q.empty() && DeltaT > 0) {
      int W = min(DeltaT, q.begin()->second);
      ans += (long long)q.begin()->first * W;
      q.begin()->second -= W;
      DeltaT -= W;
      res -= W;
      if (!q.begin()->second) q.erase(q.begin());
    }
    if (DeltaT) return -1;
    int Cnt = a[i].a, Cost = a[i].b, add = min(c - res, Cnt);
    res += add;
    while (!q.empty() && add < Cnt && Cost < q.rbegin()->first)
      if (Cnt - add >= q.rbegin()->second) {
        add += q.rbegin()->second;
        q.erase(--q.end());
      } else {
        q.rbegin()->second -= Cnt - add;
        add = Cnt;
      }
    q[Cost] += add;
  }
  return ans;
}
int main() {
  int q;
  sd(q);
  while (q--) printf("%lld\n", sol());
  return 0;
}
