#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <set>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define M 4000005
#define N 100005
using namespace std;
struct node
{
int l,r;
LL k,h;
}tr[M];
int num,t,n1,n,ds[N];
LL m,dy,a[N][4],f[N];
bool cmp(int x,int y)
{
return a[x][0]<a[y][0];
}
LL calc(LL k,LL h,LL w)
{
return k*w+h;
}
int nwp(int k)
{
if(k) return k;
tr[++n1].l=tr[n1].r=tr[n1].k=tr[n1].h=0;
return n1;
}
LL get(int k,int l,int r,int t)
{
if(l==r&&l==t) return calc(tr[k].k,tr[k].h,t);
int mid=(l+r)/2;
LL v=0;
tr[k].l=nwp(tr[k].l),tr[k].r=nwp(tr[k].r);
if(t<=mid) v=get(tr[k].l,l,mid,t);
else v=get(tr[k].r,mid+1,r,t);
return max(calc(tr[k].k,tr[k].h,t),v);
}
double dot(LL k1,LL h1,LL k2,LL h2)
{
return (h2-h1)*1.0/(k1-k2);
}
void ins(int k,int l,int r,LL dx,LL hi)
{
if(l==r)
{
if(calc(dx,hi,l)>calc(tr[k].k,tr[k].h,l)) tr[k].k=dx,tr[k].h=hi;
return;
}
if(calc(dx,hi,l)>=calc(tr[k].k,tr[k].h,l)&&calc(dx,hi,r)>=calc(tr[k].k,tr[k].h,r))
{
tr[k].k=dx,tr[k].h=hi;
return;
}
if(calc(dx,hi,l)<calc(tr[k].k,tr[k].h,l)&&calc(dx,hi,r)<calc(tr[k].k,tr[k].h,r)) return;
int mid=(l+r)/2;
tr[k].l=nwp(tr[k].l),tr[k].r=nwp(tr[k].r);
double w=dot(dx,hi,tr[k].k,tr[k].h);
bool pd=0;
if(dx>tr[k].k)
{
if(w>mid) pd=1;
else swap(tr[k].k,dx),swap(tr[k].h,hi);
}
else
{
if(w>=mid+1) swap(tr[k].k,dx),swap(tr[k].h,hi),pd=1;
}
if(!pd) ins(tr[k].l,l,mid,dx,hi);
else ins(tr[k].r,mid+1,r,dx,hi);
}
int main()
{
cin>>num>>t;
while(t--)
{
scanf("%d%lld%lld",&n,&m,&dy);
fo(i,1,n) fo(j,0,3) scanf("%lld",&a[i][j]);
fo(i,1,n) ds[i]=i;
sort(ds+1,ds+n+1,cmp);
a[n+1][0]=dy+1;
ds[n+1]=n+1;
n1=1;
tr[1].k=tr[1].h=tr[1].l=tr[1].r=0;
memset(f,0,sizeof(f));
fo(i,1,n+1)
{
int p=ds[i];
LL v=get(1,1,dy+1,a[p][0]);
f[i]=max(m,v);
if(i!=n+1&&f[i]>=a[p][1]) ins(1,1,dy+1,a[p][3],f[i]-a[p][1]+a[p][2]-a[p][3]*(a[p][0]+1));
}
printf("%lld\n",f[n+1]);
}
}