#include <bits/stdc++.h>
using namespace std;
int t, n, m, c, cur;
long long ans;
struct node {
  int t, a, b;
  node(int t = 0, int a = 0, int b = 0) : t(t), a(a), b(b) {}
} a[500005];
map<int, int> q;
int read() {
  char c = getchar();
  int x = 0;
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return x;
}
bool comp(node a, node b) { return a.t < b.t; }
long long calc(int x) {
  long long res = 0;
  while (!q.empty() && x) {
    if (x >= q.begin()->second) {
      x -= q.begin()->second;
      res += 1ll * q.begin()->second * q.begin()->first;
      cur -= q.begin()->second;
      q.erase(q.begin());
    } else {
      q.begin()->second -= x;
      res += 1ll * x * q.begin()->first;
      cur -= x;
      x = 0;
    }
  }
  return res;
}
void add(node x) {
  int step = min(c - cur, x.a);
  q[x.b] += step;
  x.a -= step;
  cur += step;
  while (!q.empty() && x.a) {
    if (q.rbegin()->first <= x.b) break;
    int step = min(x.a, q.rbegin()->second);
    q[x.b] += step;
    q.rbegin()->second -= step;
    x.a -= step;
    if (!q.rbegin()->second) q.erase(--q.end());
  }
}
int main() {
  t = read();
  while (t--) {
    n = read();
    m = read();
    c = read();
    cur = read();
    for (int i = 1; i <= n; ++i) {
      int t = read(), c = read(), b = read();
      a[i] = node(t, c, b);
    }
    sort(a + 1, a + n + 1, comp);
    a[++n].t = m;
    ans = 0;
    q.clear();
    q[0] = cur;
    bool illegal = false;
    for (int i = 1; i <= n; ++i) {
      if (cur < a[i].t - a[i - 1].t) {
        illegal = true;
        break;
      }
      ans += calc(a[i].t - a[i - 1].t);
      add(a[i]);
    }
    if (illegal)
      puts("-1");
    else
      printf("%I64d\n", ans);
  }
}
