#P1238G. Adilbek and the Watering System

    ID: 5008 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresgreedysortings*2700

Adilbek and the Watering System

No submission language available for this problem.

Description

Adilbek has to water his garden. He is going to do it with the help of a complex watering system: he only has to deliver water to it, and the mechanisms will do all the remaining job.

The watering system consumes one liter of water per minute (if there is no water, it is not working). It can hold no more than cc liters. Adilbek has already poured c0c_0 liters of water into the system. He is going to start watering the garden right now and water it for mm minutes, and the watering system should contain at least one liter of water at the beginning of the ii-th minute (for every ii from 00 to m1m - 1).

Now Adilbek wonders what he will do if the watering system runs out of water. He called nn his friends and asked them if they are going to bring some water. The ii-th friend answered that he can bring no more than aia_i liters of water; he will arrive at the beginning of the tit_i-th minute and pour all the water he has into the system (if the system cannot hold such amount of water, the excess water is poured out); and then he will ask Adilbek to pay bib_i dollars for each liter of water he has brought. You may assume that if a friend arrives at the beginning of the tit_i-th minute and the system runs out of water at the beginning of the same minute, the friend pours his water fast enough so that the system does not stop working.

Of course, Adilbek does not want to pay his friends, but he has to water the garden. So he has to tell his friends how much water should they bring. Formally, Adilbek wants to choose nn integers k1k_1, k2k_2, ..., knk_n in such a way that:

  • if each friend ii brings exactly kik_i liters of water, then the watering system works during the whole time required to water the garden;
  • the sum i=1nkibi\sum\limits_{i = 1}^{n} k_i b_i is minimum possible.

Help Adilbek to determine the minimum amount he has to pay his friends or determine that Adilbek not able to water the garden for mm minutes.

You have to answer qq independent queries.

The first line contains one integer qq (1q51051 \le q \le 5 \cdot 10^5) – the number of queries.

The first line of each query contains four integers n,m,cn, m, c and c0c_0 (0n5105,2m109,1c0c1090 \le n \le 5 \cdot 10^5, 2 \le m \le 10^9, 1 \le c_0 \le c \le 10^9) — the number of friends, the number of minutes of watering, the capacity of the watering system and the number of liters poured by Adilbek.

Each of the next nn lines contains three integers ti,ai,bit_i, a_i, b_i (0<ti<m,1aic,1bi109 0 < t_i < m, 1 \le a_i \le c, 1 \le b_i \le 10^9) — the ii-th friend's arrival time, the maximum amount of water ii-th friend can bring and the cost of 11 liter from ii-th friend.

It is guaranteed that sum of all nn over all queries does not exceed 51055 \cdot 10^5.

For each query print one integer — the minimum amount Adilbek has to pay his friends, or 1-1 if Adilbek is not able to water the garden for mm minutes.

Input

The first line contains one integer qq (1q51051 \le q \le 5 \cdot 10^5) – the number of queries.

The first line of each query contains four integers n,m,cn, m, c and c0c_0 (0n5105,2m109,1c0c1090 \le n \le 5 \cdot 10^5, 2 \le m \le 10^9, 1 \le c_0 \le c \le 10^9) — the number of friends, the number of minutes of watering, the capacity of the watering system and the number of liters poured by Adilbek.

Each of the next nn lines contains three integers ti,ai,bit_i, a_i, b_i (0<ti<m,1aic,1bi109 0 < t_i < m, 1 \le a_i \le c, 1 \le b_i \le 10^9) — the ii-th friend's arrival time, the maximum amount of water ii-th friend can bring and the cost of 11 liter from ii-th friend.

It is guaranteed that sum of all nn over all queries does not exceed 51055 \cdot 10^5.

Output

For each query print one integer — the minimum amount Adilbek has to pay his friends, or 1-1 if Adilbek is not able to water the garden for mm minutes.

Samples

Sample Input 1

4
1 5 4 2
2 4 2
0 4 5 4
2 5 3 1
1 2 4
3 1 3
2 3 5 1
2 1 1
1 4 3

Sample Output 1

6
0
-1
4