#include<stdio.h>
int main(void)
{
  int m, plan, set, money, year, part, total, top, answer[10]={0}; 
  float r;
  scanf("%d", &m);
  int i, j;

  for(i=0; i<m; i++)
    {
      scanf(" %d", &money);
      scanf(" %d", &year);
      scanf(" %d", &part);
      for(j=0; j<part; j++)
	{
	  int t_year=year;
	  scanf(" %d %f %d", &plan, &r, &set);
	  total=money;
	  if(plan==0)  //単利
	    {
	      top=0;
	      while(t_year--)
		{
		  top+=total*r;
		  total-=set;
		}
	      total+=top;    
	    }
	  else//複利
	    {
	      while(t_year--)
		{
		  total+=total*r-set;	  
		}
	    }
	  if(answer[i]<=total)
	    {
	      answer[i]=total; //最大値   
	    }
	}
    }
  for(i=0; i<m; i++) printf("%d\n", answer[i]);

  return 0;
}