#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500005;
template <typename T>
inline void read(T &AKNOI) {
  T x = 0, flag = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  AKNOI = flag * x;
}
int n, m, c, c0;
map<int, int> S;
struct Node {
  int t, a, b;
  bool operator<(const Node &rhs) const { return t < rhs.t; }
} p[MAXN];
void init() {
  read(n);
  read(m);
  read(c);
  read(c0);
  for (int i = 1; i <= n; ++i) {
    read(p[i].t);
    read(p[i].a);
    read(p[i].b);
  }
  sort(p + 1, p + n + 1);
  p[++n].t = m;
  p[n].a = p[n].b = 0;
}
long long solve() {
  long long ret = 0;
  S.clear();
  S[0] = c0;
  for (int i = 1; i <= n; ++i) {
    int nowt = p[i].t - p[i - 1].t;
    while (!S.empty() && nowt) {
      auto it = S.begin();
      int use = min(it->second, nowt);
      ret += 1LL * use * (it->first);
      nowt -= use;
      c0 -= use;
      if (((it->second) -= use) == 0) {
        S.erase(it);
      }
    }
    if (nowt) return -1;
    int pour = min(c - c0, p[i].a);
    c0 += pour;
    while (!S.empty() && pour < p[i].a && p[i].b < (S.rbegin()->first)) {
      auto it = S.rbegin();
      if (p[i].a - pour >= (it->second)) {
        pour += (it->second);
        S.erase(--S.end());
      } else {
        (it->second) -= p[i].a - pour;
        pour = p[i].a;
      }
    }
    S[p[i].b] += pour;
  }
  return ret;
}
int main() {
  int T;
  read(T);
  while (T--) {
    init();
    printf("%lld\n", solve());
  }
  return 0;
}
