#include<stdio.h>
	int main(){
		int n,mo,ye,t,x,y,sur,mem,i,j,max;
		double r;
		scanf("%d",&t);
		while(t-->0){
			max=0;
			scanf("%d",&mo);
			scanf("%d",&ye);
			scanf("%d",&n);
			for(i=0;i<n;i++){
				scanf("%d %lf %d",&x,&r,&y);
				sur=0;
				mem=mo;
				if(x==0){
					for(j=0;j<ye;j++){
						sur=sur+mem*r;
						mem-=y;
					}
					mem+=sur;
					if(mem>max)
						max=mem;
				}
				else {
					for(j=0;j<ye;j++)
						mem=mem+mem*r-y;
					
					
					if(mem>max)
						max=mem;
					
					
				}
			}
			printf("%d\n",max);
		}
		return 0;
}