#include<iostream>
#include<cstdio>
#include<algorithm>
//#define int long long
using namespace std;
long long n,k;
struct node{
long long num,val,cos;
}e[100005];
bool cmp(node x,node y)
{
return x.cos>y.cos;
}
int main()
{
long long T;
cin>>T;
for(int i=1;i<=T;i++)
{
cin>>n>>k;
long long he=0;
for(int i=1;i<=n;i++)
{
cin>>e[i].num>>e[i].val>>e[i].cos;
he+=e[i].num*e[i].val;
}
sort(e+1,e+1+n,cmp);
long long ans=0;
long long i=1;
long long v=0;
long long co=e[i].cos;
while(he>k)
{
v+=e[i].val;
e[i].num--;
if(v>k)
{
he-=v;
he+=e[i].val;
v=e[i].val;
ans+=co;
co=e[i].cos;
}
if(e[i].num==0)
i++;
}
ans+=co;
cout<<ans<<endl;
}
}