#include<stdio.h>

int main(void)
{
	int m,i,j,k;
	int fund,year,type;
	int check,tesu;
	double nenri;
	int temp,temp1,max;
	int finfund[100];
	
	scanf("%d",&m);
	for(i=0;i<m;i++)
	{
		scanf("%d",&fund);
		scanf("%d",&year);
		scanf("%d",&type);
		max = 0;
		for(j=0;j<type;j++)
		{
			scanf("%d %lf %d",&check,&nenri,&tesu);
			if(check==0)
			{
				temp = fund;
				temp1 = 0;
				for(k=0;k<year;k++)
				{
					temp1+=(int)(temp*nenri);
					temp-=tesu;
				}
				//printf("%d\n",temp+temp1);
				finfund[j] = temp+temp1;
			}
			else
			{
				temp = fund;
				temp1 = 0;
				for(k=0;k<year;k++)
				{
					temp1=(int)(temp*nenri);
					temp+=temp1-tesu;
				}
				//printf("%d\n",temp);
				finfund[j] = temp;
			}
			if(max<finfund[j])
			max=finfund[j];
		}
		printf("%d\n",max);
	}
	return 0;
}