#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
template <typename T>
inline void Read(T &t) {
  int c = getchar(), f = 0;
  for (; c < '0' || c > '9'; c = getchar()) f = (c == '-');
  for (t = 0; c >= '0' && c <= '9'; c = getchar())
    t = (t << 3) + (t << 1) + (c ^ 48);
  if (f) t = -t;
}
const int N = 5e5 + 5;
int test;
long long n, c, cap[N];
struct Friend {
  long long t, a, p;
  bool operator<(const Friend &rhs) const { return t < rhs.t; }
} f[N];
struct Node {
  long long p, id;
  Node() { p = 0, id = 0; }
  Node(long long __p, long long __id) { p = __p, id = __id; }
  bool operator<(const Node &rhs) const { return p < rhs.p; }
  bool operator>(const Node &rhs) const { return p > rhs.p; }
  bool operator<=(const Node &rhs) const { return p <= rhs.p; }
  bool operator>=(const Node &rhs) const { return p >= rhs.p; }
  void clear() { p = 0, id = 0; }
};
struct my_priority_queue {
 private:
  int size, num[N];
  Node heap[N];

 public:
  my_priority_queue() { size = 0; }
  bool empty() { return !size; }
  void clear(int limit) {
    size = 0;
    for (int i = 1; i <= limit; i++) num[i] = 0, heap[i].clear();
  }
  Node top() { return heap[1]; }
  void push(Node x) {
    heap[++size] = x, num[x.id] = size;
    int now = size, next;
    while (now > 1) {
      next = now >> 1;
      if (heap[now] <= heap[next]) break;
      num[heap[now].id] = next, num[heap[next].id] = now;
      swap(heap[now], heap[next]);
      now = next;
    }
  }
  void pop(int id) {
    int now = num[id], next;
    heap[now] = heap[size--], num[heap[now].id] = now;
    if (now > 1 && heap[now] > heap[now >> 1])
      while (now > 1) {
        next = now >> 1;
        if (heap[now] <= heap[next]) break;
        num[heap[now].id] = next, num[heap[next].id] = now;
        swap(heap[now], heap[next]);
        now = next;
      }
    else
      while ((now << 1) <= size) {
        next = now << 1;
        if (next < size && heap[next | 1] > heap[next]) next |= 1;
        if (heap[now] >= heap[next]) break;
        num[heap[now].id] = next, num[heap[next].id] = now;
        swap(heap[now], heap[next]);
        now = next;
      }
  }
} q1, q2;
void work() {
  long long now, ans = 0, last = 0;
  Read(n), Read(f[n + 1].t), Read(c), Read(now);
  for (int i = 1; i <= n; i++) Read(f[i].t), Read(f[i].a), Read(f[i].p);
  for (int i = 1; i <= n; i++) cap[i] = 0;
  sort(f + 1, f + n + 1);
  q1.push(Node(0, 0)), q2.push(Node(0, 0)), cap[0] = now;
  for (int i = 1; i <= n + 1; i++) {
    long long temp = f[i].t - last, a = f[i].a, p = f[i].p;
    last = f[i].t;
    if (temp > now) {
      q1.clear(n), q2.clear(n), puts("-1");
      return;
    }
    now -= temp;
    while (temp > 0) {
      long long id = q2.top().id, __p = -(q2.top().p);
      if (temp >= cap[id])
        ans += cap[id] * __p, temp -= cap[id], q1.pop(id), q2.pop(id);
      else
        ans += temp * __p, cap[id] -= temp, temp = 0;
    }
    if (i == n + 1) break;
    if (a + now <= c) {
      now += a, cap[i] = a, q1.push(Node(p, i)), q2.push(Node(-p, i));
      continue;
    }
    a -= c - now, cap[i] = c - now, now = c;
    while (a > 0 && !q2.empty()) {
      long long id = q1.top().id, __p = q1.top().p;
      if (p >= __p) break;
      if (a >= cap[id])
        a -= cap[id], cap[i] += cap[id], q1.pop(id), q2.pop(id);
      else
        cap[id] -= a, cap[i] += a, a = 0;
    }
    q1.push(Node(p, i)), q2.push(Node(-p, i));
  }
  q1.clear(n), q2.clear(n);
  printf("%lld\n", ans);
}
int main() {
  Read(test);
  while (test--) work();
  return 0;
}
