
main(N,R,M,m,y,n,t,c,i,d){double r;
	for(scanf("%d",&N);N--;printf("%d\n",R)){
		for(scanf("%d%d%d",&M,&y,&n),R=M;n--;R=m>R?m:R){
			if(m=M,d=0,scanf("%d%lf%d",&t,&r,&c),t){ //fukuri
				for(i=0;i<y;i++){
					m+=(int)(r*m)-c;
				}
			}else{ //tanri
				for(i=0;i<y;i++){
					d+=(int)(r*m);
					m=m-c;
				}
				m+=d;
			}
		}
	}
exit(0);}