main(){
	int amount,year,n,Amount;
	scanf("%*d");
	for(;~scanf("%d%d%d",&amount,&year,&n);){
		Amount=0;
		for(;n--;){
			int type,fee,a,simple,y;
			double rate;
			scanf("%d%lf%d",&type,&rate,&fee);
			a=amount;
			simple=0;
			for(y=0;y<year;y++){
				if(type==0)
					simple+=a*rate;
				else
					a+=a*rate;
				a-=fee;
			}
			a+=simple;
			if(a>Amount)
				Amount=a;
		}
		printf("%d\n",Amount);
	}
	exit(!puts(""));
}