#include<stdio.h>
int main(void){
    int m;
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        int fund,year,n;
        int max=0;
        scanf("%d%d%d",&fund,&year,&n);
        for(int j=0;j<n;j++){
            int flag,fee;
            double r;
            scanf("%d%lf%d",&flag,&r,&fee);
            int A=fund,B,balance=fund;
            int sum=0;
            for(int k=0;k<year;k++){
                if(flag){
                    A=balance;
                    B=A*r;
                    balance=A+B-fee;
                }
                else{
                    A=balance;
                    B=A*r;
                    balance=A-fee;
                    sum+=B;
                }
            }
            if(flag){
                if(max<balance) max=balance;
            }else{
                if(max<balance+sum) max=balance+sum;
            }
        }
        printf("%d\n",max);
    }
}

