#include<stdio.h>

int SimpleInterest(int, int, double, int);
int ComposedInterest(int, int, double, int);

int main(){
  int i, j, n, m, x, y, isInt, fee, max, _max;
  double rate;

  scanf("%d", &n);

  for(i=0; i<n; i++){
    scanf("%d %d %d", &x, &y, &m);

    for(j=0, max=x; j<m; j++){
      scanf("%d %lf %d", &isInt, &rate, &fee);

      if(isInt == 0) _max = SimpleInterest(x, y, rate, fee);
      else _max = ComposedInterest(x, y, rate, fee);

      if(max < _max) max = _max;
    }

    printf("%d\n", max);
  }
  return 0;
}

int SimpleInterest(int x, int year, double rate, int fee){
  int i, _x;
  for(i=0, _x=0; i<year; i++){
    _x += (x * rate);
    x -= fee;
  }
  x += _x;
  return x;
}

int ComposedInterest(int x, int year, double rate, int fee){
  int i, _x;
  for(i=0; i<year; i++){
    _x = x * rate;
    x += (_x - fee);
  }
  return x;
}