#include <stdio.h>
int calc(int type,int y,int init,double rate,int c){
	int i,b=0;
	if(type==0){
		for(i=0;i<y;i++){
			b+=init*rate;
			init-=c;
		}
		return init+b;
	}
	else{
		for(i=0;i<y;i++){
			b=init*rate;
			init=init+b-c;
		}
		return init;
	}
}
int main(void){
	int m,ans,init,y,n,t,c,tmp;
	double r;
	scanf("%d",&m);
	while(m--){
		ans=0;
		scanf("%d %d %d",&init,&y,&n);
		while(n--){
			scanf("%d %lf %d",&t,&r,&c);
			tmp=calc(t,y,init,r,c);
			if(ans<tmp)
				ans=tmp;
		}
		printf("%d\n",ans);
	}
	return 0;
}