#include<iostream>
#include<cstdio>
#include<algorithm>
#define int long long
using namespace std;
int n,k;
struct node{
int num,val,cos;
}e[100005];
bool cmp(node x,node y)
{
return x.cos>y.cos;
}
signed main()
{
int T;
cin>>T;
for(int i=1;i<=T;i++)
{
cin>>n>>k;
int 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);
int ans=0;
int i=1;
int v=0;
int 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;
}
}