require 'scanf'

simple = lambda do |m, i, rate, fee|
  return m - fee, i + (m*rate).truncate
end

compound = lambda do |m, i, rate, fee|
  return m + (m*rate).truncate - fee, 0
end

all = gets.to_i
all.times do
  m = gets.to_i
  y = gets.to_i
  n = gets.to_i

  results = Array.new
  n.times do
    money, interest = m, 0
    type, rate, fee = gets.scanf("%d %f %d")

    func = type == 0 ? simple : compound
    y.times { money, interest = func.call( money, interest, rate, fee ) }

    results << ( money + interest )
  end
  puts results.max
end