#include <stdio.h>

int main(void)
{
    int m, i;
    scanf("%d", &m);

    for(i = 0; i < m; i++){
        int init, year, n, max = 0, j;

        scanf("%d", &init);
        scanf("%d", &year);
        scanf("%d", &n);
        for(j = 0; j < n; j++){
            int int_t, fee;
            int money = init;
            double interest;
            scanf("%d %lf %d", &int_t, &interest, &fee);

            if(int_t == 0){
                int k, pool = 0;
                for(k = 0; k < year; k++){
                    pool += (int)(money * interest);
                    money -= fee;
                }
                money += pool;
            }else if(int_t == 1){
                int k;
                for(k = 0; k < year; k++){
                    money += (int)(money * interest);
                    money -= fee;
                }
            }
            if(money > max)
                max = money;
        }
        printf("%d\n", max);
    }

    return 0;
}