import std.stdio, std.conv, std.string, std.algorithm, std.math;

int calcSimple( int y, int money, double rate, int commission ) {
  int ans = 0;
  foreach ( i; 0 .. y ) {
	ans += to!int( money * rate );
	money -= commission;
  }
  return ans + money;
}

int calcCompound( int y, int money, double rate, int commission ) {
  foreach ( i; 0 .. y ) {
	money = to!int( money * ( 1 + rate ) );
	money -= commission;
  }
  return money;
}

void main()
{
  int m = to!int( strip( readln ) );
  foreach ( i; 0 .. m ) {
	int money = to!int( strip( readln ) );
	int y = to!int( strip( readln ) );
	int n = to!int( strip( readln ) );
	int ans = 0;
	foreach ( j; 0 .. n ) {
	  
	  auto line = split( strip( readln ), " " );
	  int isCompoundInterest = to!int( line[ 0 ] );
	  double rate = to!double( line[ 1 ] );
	  int commission = to!int( line[ 2 ] );
	  if ( isCompoundInterest == 0 ) {
		ans = max( ans, calcSimple( y, money, rate, commission ) );
	  } else {
		ans = max( ans, calcCompound( y, money, rate, commission ) );
	  }
	}
	writeln( ans );
  }
}