#include <stdio.h>
int n;
int main(){
	scanf("%d",&n);
	while(n--){
		int money,year,way;
		double rate;
		scanf("%d",&money);
		scanf("%d",&year);
		scanf("%d",&way);
		int c,d=0,ans;

		while(way--){
			int flag,factorage;
			int y=year;
			scanf("%d",&flag);
			scanf("%lf",&rate);
			scanf("%d",&factorage);
                if(flag==0){
				int a=money,b,q=0;
				while(y--){
					b=(int)a*rate;
					a-=factorage;
					q+=b;
				}
				a+=q;
				c=a;
			}
			if (flag==1) {
				int  a=money,b;
				while (y--) {
					b=(int)a*rate;
					a=a+b-factorage;
					}
				c=a;
			}
			if (c>d) {
				d=c;
			}
			
		}
		printf("%d",d);		
	}
	return 0;
	
}

