#include <stdio.h>

int simple(int initial, int year, int charge, double rate){ //単利
  int i, interest = 0;
  for(i = 0; i < year; i++){
    interest += initial * rate;
    initial -= charge;
  }
  return initial + interest;
}

int compound(int initial, int year, int charge, double rate){ //複利
  int i, interest;
  for(i = 0; i < year; i++){
    interest = initial * rate;
    initial = initial + interest - charge;
  }
  return initial;
}

int Max(int a, int b){
  if(a > b)
    return a;
  else
    return b;
}

void solve(){
  int initial, year, n, s_c, charge;
  int i, result = 0, buf;
  double rate;
  
  scanf("%d", &initial);
  scanf("%d", &year);
  scanf("%d", &n);
  
  for(i = 0; i < n; i++){
    scanf("%d %lf %d", &s_c, &rate, &charge);
    if(s_c == 0) result = Max(result, simple(initial, year, charge, rate));
    else result = Max(result, compound(initial, year, charge, rate));
  }
  printf("%d\n", result);
}

int main(void){
  int m, i;
  scanf("%d", &m);

  for(i = 0; i < m; i++){
    solve();
  }

  return 0;
}
