#include <stdio.h>
int tanri(int src, int year, double riritu, int tesuu)
{
  int i,risi=0;
  for(i=0; i<year; i++){
    risi += (int)(src * riritu);
    src -= tesuu;
  }
  return src + risi;
}
int fukuri(int src, int year, double riritu, int tesuu)
{
  int i;
  for(i=0; i<year; i++){
    src += (int)(src * riritu);
    src -= tesuu;
  }
  return src;
}
int main()
{
  int m,i;
  scanf("%d",&m);
  for(i=0; i<m; i++){
    int src, year,n,j,max = 0;
    scanf("%d\n%d\n%d",&src,&year,&n);
    for(j=0; j<n; j++){
      int k, tesuu,ret;
      double riritu;
      scanf("%d %lf %d",&k,&riritu,&tesuu);
      if(k == 0){
        ret = tanri(src,year,riritu,tesuu);
      }else{
        ret = fukuri(src,year,riritu,tesuu);
      }
      if(max < ret){
        max = ret;
      }
    }
    printf("%d\n",max);
  }
  return 0;
}