#include<stdio.h>
#include<stdlib.h>
 
int main(void)
{
 
  int i,j;
  int fmoney,lmoney;
  int r;
  int year;
  int b,cost;
  double per;
  int m,n;
  int *a;
 
  scanf("%d",&m);
  while(1)
    {
      if(m==0)break;
      scanf("%d",&fmoney);
      scanf("%d",&year);
      scanf("%d",&n);
      a=(int *)malloc(sizeof(int)*n);
 
      for(i=0;i<n;i++)
    {
      scanf("%d %lf %d",&b,&per,&cost);
      lmoney=fmoney;
           r=0;
      if(b==0)
        {
          for(j=0;j<year;j++)
        {
          r+=(int)(lmoney*per);
          lmoney-=cost;
        }
          a[i]=r+lmoney;
        }
 
      if(b==1)
        {
          for(j=0;j<year;j++)
        {
          r = (int)(lmoney*per);
                  lmoney=lmoney+r-cost;
        }
          a[i]=lmoney;
        }
    }
      for(i=1;i<n;i++)
    {
      if(a[0]<a[i]){a[0]=a[i];}
    }
      printf("%d\n",a[0]);
      m--;
    }
 
  return 0;
}
