#include <bits/stdc++.h>
using namespace std;
const int arr = 2e5 + 10;
const int ar = 2e3 + 10;
const long double pi = acos(-1);
const long double eps = 1e-10;
const long long md = 1e9 + 7;
int t[(int)(5e5 + 10)];
int a[(int)(5e5 + 10)];
int b[(int)(5e5 + 10)];
vector<int> vh[(int)(5e5 + 10)];
int tt[(int)(5e5 + 10)];
int tree[4 * (int)(5e5 + 10)];
int push[4 * (int)(5e5 + 10)];
void clear(int v, int l, int r) {
  if (v >= 4 * (int)(5e5 + 10)) {
    while (1) {
    }
  }
  tree[v] = 0;
  push[v] = 0;
  if (l == r) {
    return;
  }
  int m = (l + r) / 2;
  clear(v * 2, l, m);
  clear(v * 2 + 1, m + 1, r);
}
void make_push(int v) {
  if (v * 2 + 1 >= 4 * (int)(5e5 + 10)) {
    while (1) {
    }
  }
  tree[v * 2] += push[v];
  tree[v * 2 + 1] += push[v];
  push[v * 2] += push[v];
  push[v * 2 + 1] += push[v];
  push[v] = 0;
}
void upd1(int v, int l, int r, int pos, int val) {
  if (v >= 4 * (int)(5e5 + 10)) {
    while (1) {
    }
  }
  if (l == r) {
    tree[v] = val;
    return;
  }
  int m = (l + r) / 2;
  make_push(v);
  if (pos <= m) {
    upd1(v * 2, l, m, pos, val);
  } else {
    upd1(v * 2 + 1, m + 1, r, pos, val);
  }
  tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}
void upd2(int v, int l, int r, int tl, int tr, int val) {
  if (v >= 4 * (int)(5e5 + 10)) {
    while (1) {
    }
  }
  if (l > tr || r < tl) {
    return;
  }
  if (l >= tl && r <= tr) {
    tree[v] += val;
    push[v] += val;
    return;
  }
  int m = (l + r) / 2;
  make_push(v);
  upd2(v * 2, l, m, tl, tr, val);
  upd2(v * 2 + 1, m + 1, r, tl, tr, val);
  tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
}
int get(int v, int l, int r, int tl, int tr) {
  if (v >= 4 * (int)(5e5 + 10)) {
    while (1) {
    }
  }
  if (l > tr || r < tl) {
    return 0;
  }
  if (l >= tl && r <= tr) {
    return tree[v];
  }
  int m = (l + r) / 2;
  make_push(v);
  return max(get(v * 2, l, m, tl, tr), get(v * 2 + 1, m + 1, r, tl, tr));
}
void solve() {
  int n, m, C, c0;
  cin >> n >> m >> C >> c0;
  vector<int> T(0);
  for (int i = 1; i <= n; i++) {
    cin >> t[i] >> a[i] >> b[i];
    T.push_back(t[i]);
  }
  T.push_back(0);
  T.push_back(m);
  sort(T.begin(), T.end());
  if ((int)(T.size()) >= (int)(5e5 + 10) - 3) {
    while (1) {
    }
  }
  for (int i = 0; i < (int)(T.size()); i++) {
    vh[i].clear();
  }
  for (int i = 1; i <= n; i++) {
    tt[i] = lower_bound(T.begin(), T.end(), t[i]) - T.begin();
    vh[tt[i]].push_back(i);
  }
  int cur = c0;
  set<pair<pair<int, int>, int>> setik;
  long long ans = 0;
  clear(1, 0, (int)(T.size()) - 1);
  for (int i = 0; i + 1 < (int)(T.size()); i++) {
    upd1(1, 0, (int)(T.size()) - 1, i, cur);
    int L = T[i + 1] - T[i];
    if (L > C) {
      cout << -1 << "\n";
      return;
    }
    for (auto j : vh[i]) {
      setik.insert(make_pair(make_pair(b[j], tt[j]), j));
    }
    while (cur < L && !setik.empty()) {
      int best = setik.begin()->second;
      int can = min(
          {L - cur, a[best], C - get(1, 0, (int)(T.size()) - 1, tt[best], i)});
      if (can < 0) {
        while (1) {
        }
      }
      if (!can) {
        setik.erase(setik.begin());
      } else {
        ans += 1ll * can * b[best];
        cur += can;
        a[best] -= can;
        upd2(1, 0, (int)(T.size()) - 1, tt[best], i, can);
      }
    }
    if (cur < L) {
      cout << -1 << "\n";
      return;
    }
    cur -= L;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int q;
  cin >> q;
  while (q--) {
    solve();
  }
}
