#include <stdio.h>

int main(void){
  int i,m,n,first,year,flg,dec,rst,max,sum;
  double per;

  scanf("%d",&m);
  while(m--){
    scanf("%d%d%d",&first,&year,&n);
    max = 0;
    while(n--){
      scanf("%d%lf%d",&flg,&per,&dec);
      rst = first;
      for(i=sum=0;i<year;i++){
	if(flg){
	  rst = rst + (int)(rst*per) - dec;
	}
	else{
	  sum += (int)(rst*per);
	  rst = rst - dec;
	}
      }
      if(!flg) rst+=sum;
      if(rst > max) max=rst;
    }
    printf("%d\n",max);
  }

  return 0;
}