#include<stdio.h>
int main(){
	int n,m,i,j,k,y,w;
	int _money,money,charge;
	double inter;
	int moneyMax;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&_money);
		scanf("%d",&y);
		scanf("%d",&m);
		moneyMax=-1;
		for(j=0;j<m;j++){
			money=_money;
			scanf("%d %lf %d",&w,&inter,&charge);
			if(w){
				for(k=0;k<y;k++)
					money=money*(1.0+inter)-charge;
			}
			else{
				int interSum=0;
				for(k=0;k<y;k++){
					interSum+=money*(inter);
					money-=charge;
				}
				money+=interSum;
			}
			if(moneyMax<money)moneyMax=money;
		}
		printf("%d\n",moneyMax);
	}
	return 0;
}