# hdu2191 悼念512汶川大地震遇难同胞——珍惜现在，感恩生活（多重背包+二进制优化模板）

#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <iostream>

using namespace std;

typedef long long LL;

const int N = 1005;
const int INF = 0x3f3f3f3f;

int dp[N], V;

void ZeroOnePack(int cost, int weight)
{
for(int j = V; j >= cost; j--)
dp[j] = max(dp[j], dp[j-cost]+weight);
}

void CompletePack(int cost, int weight)
{
for(int j = cost; j <= V; j++)
dp[j] = max(dp[j], dp[j-cost]+weight);
}

void MultiplePack(int cost, int weight, int amount)
{
if(cost*amount>=V)
{
CompletePack(cost, weight);
return;
}
int k = 1;
while(k < amount)
{
ZeroOnePack(cost*k, weight*k);
amount-=k;//注意在前面
k=k*2;
}
ZeroOnePack(amount*cost, amount*weight);
}

int main()
{
// freopen("in.txt", "r", stdin);
int t, n;
int cost[N], weight[N], amount[N];
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &V, &n);
for(int i = 1; i <= n; i++)
scanf("%d%d%d", &cost[i], &weight[i], &amount[i]);
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++)
MultiplePack(cost[i], weight[i], amount[i]);
/*  for(int i = 0; i <= V; i++)
printf("%d ", dp[i]);
printf("\n");*/
printf("%d\n", dp[V]);
}
return 0;
}