#include <stdio.h>

int way1(int money,int year, double percent, int minus)
{
	int x;
	for(x=0;x<year;x++){
		money=money*(1.0+percent)-minus;
	}
	return money;
}

int way2(int money, int year, double percent, int minus)
{
	int y;
	int plus=0;
	for(y=0;y<year;y++){
		plus+=money*percent;
		money=money-minus;
	}
	return money+plus;
}

int main(void)
{
	int m;
	int money;
	int year;
	int num_way;
	int way;
	double percent;
	int minus;
	int count1;
	int count2;
	
	scanf("%d",&m);
	for(count1=0; count1<m;count1++){
		int max=0;
		scanf("%d",&money);
		scanf("%d",&year);
		scanf("%d",&num_way);
		for(count2=0;count2<num_way;count2++){
			scanf("%d",&way);
			scanf("%lf",&percent);
			scanf("%d",&minus);
			if (way == 1) {
				if (max < way1(money, year, percent, minus))
					max = way1(money, year, percent, minus);
			}
			else {
				if (max < way2(money, year, percent, minus))
					max = way2(money, year, percent, minus);
			}
		}
		printf("%d\n",max);
	}
	return 0;
}
