#include<stdio.h>
int N,M,Y,P,F,FEE,i,res,tmp;
double RATE;

int h(int m,int y)
{
	if(y==Y)
		return m;
	else
		return h(m+m*RATE-FEE,y+1);
}

int t(int m,int y)
{
	if(y==Y)
		return 0;
	return t(m-FEE,y+1)+m*RATE;
}

int main()
{
	for(scanf("%d",&N);N--;)
	{
		scanf("%d%d%d",&M,&Y,&P);
		for(res=0;P--;)
		{
			scanf("%d%lf%d",&F,&RATE,&FEE);
			tmp=F?h(M,0):M-Y*FEE+t(M,0);
			res=res<tmp?tmp:res;
		}
		printf("%d\n",res);
	}
	return 0;
}