/*
AizuOnline A1135
Title Ohgas' Fortune
*/
#include <stdio.h>
#include <float.h>
//#include <math.h>
//#include <limits.h>
//Global data section
int m;
int init_fund;
int op_years;
int n;
int s_or_c[100];
double rate[100];
int  charge[100];
int final_amount[100];
//
int calc_compound(int amount,int years,double rat,int chg)
{
        if(years==0)
                return(amount);
        amount = amount*(1.0 + rat);
        amount -= chg;
        return( calc_compound( amount, years-1, rat, chg));
}
int calc_simple(int amount,int interest,int years,double rat,int chg)
{
        if(years==0)
        {
                //printf("%d %d %d",amount,interest,amount+interest);
                return(amount+interest);
        }
        interest+=amount*rat;
        amount -= chg;
        return(calc_simple(amount,interest,years-1,rat,chg));
}
int calc(int num)
{
        int i,amount,ret;
        amount=init_fund;
 
        if(s_or_c[num]==0)
                ret=calc_simple(amount,0,op_years,rate[num],charge[num]);
        else
                ret=calc_compound(amount,op_years,rate[num],charge[num]);
}
int solve()
{
        int i;
        int max_final_amount;

        for(i=0;i<n;i++)
                final_amount[i]=calc(i);
        max_final_amount=0;
        for(i=0;i<n;i++)
                if(final_amount[i]>max_final_amount)
                        max_final_amount=final_amount[i];

        return(max_final_amount);
}
main()
{
        int i,j,ret;

        scanf("%d",&m);

        for(i=0;i<m;i++)
        {
                scanf("%d",&init_fund);
                scanf("%d",&op_years);
                scanf("%d",&n);

                //printf("%d %d %d\n",init_fund,op_years,n);

                for(j=0;j<n;j++)
                        scanf("%d %lf %d",
                              &s_or_c[j],
                              &rate[j],
                              &charge[j]);
                //printf("S_C:%d R:%lf C:%d\n",s_or_c[0],rate[0],
                //              charge[0]);
                ret=solve();
                printf("%d\n",ret);
        }
return(0);
}