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

#define MAX(A,B) \
  ( (B) > (A) ? (B) : (A) )

int
main (
  int   argc,
  char *argv[ ]
  )
{
  int dsn;
  int i, j;

  scanf ( "%d", &dsn );
  while ( dsn-- )
  {
    int init, years, wn;
    int res = 0;

    scanf ( "%d%d%d", &init, &years, &wn );
    while ( wn-- )
    {
      int type, reqs;
      double mag;

      scanf ( "%d%lf%d", &type, &mag, &reqs );
      if ( type == 0 )
      { /* 単利の場合 */
        int cur = init, adx = 0;

        for ( i = 0; i < years; ++i )
        {
          adx += (int)( cur * mag );
          cur -= reqs;
        }
        res = MAX( res, cur + adx );
      }
      else
      { /* 複利の場合 */
        int cur = init;

        for ( i = 0; i < years; ++i )
        {
          cur = (int)( cur + cur * mag - reqs );
        }
        res = MAX( res, cur );
      }
    }

    printf ( "%d\n", res );
  }

  return ( EXIT_SUCCESS );
}