#include <iostream>
using namespace std;
int nCases;
int nValue, nKind;
int value[105], weight[105], bag[105]; //价值,容量,个数
int nMultiplePack[105]; //DP
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d", &nCases);
while(nCases--)
{
memset(nMultiplePack, 0, sizeof(nMultiplePack));
scanf("%d %d", &nValue, &nKind);
for(int i=0; i<nKind; ++i)
scanf("%d %d %d", &value[i], &weight[i], &bag[i]);
for(int i=0; i<nKind; ++i)
for(int j=0; j<bag[i]; ++j)
for(int k=nValue; k>=value[i]; --k)
if(nMultiplePack[k] < nMultiplePack[k-value[i]]+weight[i])
nMultiplePack[k] = nMultiplePack[k-value[i]] + weight[i];
printf("%d\n", nMultiplePack[nValue]);
}
return 0;
}