#include<stdio.h>
#include<stdlib.h>

int main(void)
{
  int m; 
  int money, year, part;
  int  total, top, answer[10]={0};
  int plan, set;
  int i, k, j;
  float r;
  scanf("%d", &m);
  for(i=0; i<m; i++)
    {
      scanf("%d", &money);
      scanf("%d", &year);
      scanf("%d", &part);

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

	 
	}
    }
  for(i=0; i<m;i++)
  printf("%d\n", answer[i]);

  return 0;
}