#include<stdio.h>

int main(void){
	int m,i,j,k,inimoney,money,year,n,sw,com,ef,max;
	double rate,efnow;

	scanf("%d",&m);
	for(i=0;i<m;i++){
		scanf("%d",&inimoney);
		scanf("%d",&year);
		scanf("%d",&n);
		max=0;
		for(j=0;j<n;j++){
			scanf("%d %lf %d",&sw,&rate,&com);
			ef=0; money=inimoney;
			for(k=0;k<year;k++){
				if(sw==1){
					money=money*(1.0+rate)-com;
				}else{
					ef=money*rate;
					money=money-com;
				}
			}
			if(sw==0) money=money+ef;
			if(max<money) max=money;
		}
		printf("%d\n",max);
	}

	return 0;
}