#include<stdio.h>
int main(void)
{
    int m,n,money,year,type,commission,account,pool,max;
    double rate;
    int i,j,k;

    scanf("%d",&m);

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

        max=0;
        for(j=0;j<n;j++)
        {
            scanf("%d %lf %d",&type,&rate,&commission);

            account=money;
            if(type==0)
            {
                pool=0;
                for(k=0;k<year;k++)
                {
                    pool+=(double)account*rate;
                    account-=commission;
                }
                account+=pool;
            }
            else
            {
                for(k=0;k<year;k++)
                {
                    account=(double)account*(1.0+rate)-commission;
                }
            }
            if(account>max)
            {
                max=account;
            }
        }
        printf("%d\n",max);
    }
    return 0;
}