#include <stdio.h>

int main(void)
{
	int m,n,money,year,system,cost,interest=0,total=0,last=0;
	int interest2[10],intererst_total=0;
	double rate;
	int i,j,k;
	scanf("%d",&m);
	for(i=0;i<m;i++){
		scanf("%d",&money);
		scanf("%d",&year);
		scanf("%d",&n);
		total=money;
		for(j=0;j<n;j++){
			scanf("%d %lf %d",&system,&rate,&cost);
			if(system==1){
				interest=total*rate;
				total=total+interest-cost;
			}else if(system==0){
				for(k=0;k<n;k++){
					interest2[k]=total*rate;
					total=total-cost;
					interest_total=interest_total+interest[k];
				}
			}
		}
		printf("%d",total);
	}
}
	