#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,m,p[100+5],h[100+5],c[100+5],dp[100+5];
void zeroonepack(int cost,int weight)
{
for(int i=n;i>=cost;i--)
{
dp[i]=max(dp[i],dp[i-cost]+weight);
}
}
void completepack(int cost,int weight)
{
for(int i=cost;i<=n;i++)
{
dp[i]=max(dp[i],dp[i-cost]+weight);
}
}
void multiplepack(int cost,int weight,int amount)
{
if(amount*cost>=n)
completepack(cost,weight);
else
{
int k=1;
while(k<amount)
{
zeroonepack(cost*k,weight*k);
amount-=k;
k*=2;
}
zeroonepack(cost*amount,weight*amount);
}
}
int main()
{
int _,i,j;
scanf("%d",&_);
while(_--)
{
scanf("%d%d",&n,&m);
for(i=0;i<m;i++)
scanf("%d%d%d",&p[i],&h[i],&c[i]);
memset(dp,0,sizeof(dp));
for(i=0;i<m;i++)
{
multiplepack(p[i],h[i],c[i]);
}
printf("%d
",dp[n]);
}
return 0;
}