#include<stdio.h>
int main(void)
{
  int m, plan, set, money, year, part, total, top, answer[100]={0}; 
  float r;
  scanf(" %d", &m);
  int t_m = m;
  while(t_m--)
    {
      scanf(" %d", &money);
      scanf(" %d", &year);
      scanf(" %d", &part);
      //      printf("money=%d, year=%d, part=%d\n", money, year, part);
      while(part--)
	{
	  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[t_m]<=total)
	    {
	      answer[t_m]=total; //?????§???   
	    }
	}
    }
  while(m--) printf("%d\n", answer[m]);

  return 0;
}