#include <bits/stdc++.h>
using namespace std;
const int N = 500 * 1000 + 13;
int n, m, c, c0;
pair<int, pair<int, int> > a[N];
long long solve() {
  scanf("%d%d%d%d", &n, &m, &c, &c0);
  for (int i = 0; i < int(n); ++i)
    scanf("%d%d%d", &a[i].first, &a[i].second.first, &a[i].second.second);
  a[n++] = make_pair(m, make_pair(0, 0));
  sort(a, a + n);
  int sum = c0;
  map<int, int> q;
  q[0] = c0;
  long long ans = 0;
  for (int i = 0; i < int(n); ++i) {
    int first = a[i].first;
    int cnt = a[i].second.first;
    int cost = a[i].second.second;
    int dist = first - (i ? a[i - 1].first : 0);
    while (!q.empty() && dist > 0) {
      int can = min(q.begin()->second, dist);
      ans += q.begin()->first * 1ll * can;
      sum -= can;
      dist -= can;
      q.begin()->second -= can;
      if (q.begin()->second == 0) q.erase(q.begin());
    }
    if (dist > 0) return -1;
    int add = min(c - sum, cnt);
    sum += add;
    while (add < cnt && !q.empty() && q.rbegin()->first > cost) {
      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;
  scanf("%d", &q);
  for (int i = 0; i < int(q); ++i) cout << solve() << endl;
}
