#include <stdio.h>
#include <stdlib.h>

float estimate_Tanri(float operatingfund,float rate_of_interest,int period,int fee);
float estimate_Fukuri(float operatingfund,float rate_of_interest,int period,int fee);

int main(void) {
	int m;
	scanf("%d",&m);
	int i;
	for(i=0;i<m;i++){
		float operatingfund;
		scanf("%f",&operatingfund);
		int period;
		scanf("%d",&period);

		int n;						//?????¨??????????¨??????°
		scanf("%d",&n);
		float result = 0;
		int j;
		for(j=0;j<n;j++){
			int TorF;
			float rate_of_interest;
			int fee;
			scanf("%d %f %d",&TorF,&rate_of_interest,&fee);

			int tmp;
			if(TorF == 0){
				tmp = estimate_Tanri(operatingfund,rate_of_interest,period,fee);
			}
			else{
				tmp = estimate_Fukuri(operatingfund,rate_of_interest,period,fee);
			}

			if(result<=tmp){
				result = tmp;
			}
		}
		printf("%d\n",result);
	}
	return 0;
}

float estimate_Tanri(float operatingfund,float rate_of_interest,int period,int fee){
	int i;
	float total_interest = 0;
	for(i=0;i<period;i++){
		total_interest += (float)(int)(operatingfund * rate_of_interest);
		operatingfund -= (float)fee;
	}
	return total_interest + operatingfund;
}

float estimate_Fukuri(float operatingfund,float rate_of_interest,int period,int fee){
	int i;
	for(i=0;i<period;i++){
		operatingfund = operatingfund + (float)(int)(operatingfund * rate_of_interest) - (float)fee;
	}
	return operatingfund;
}