gets.to_i.times do # each dataset
  money = gets.to_i
  year  = gets.to_i

  max = 0
  gets.to_i.times do  # each 運用方法
    m = money

    line = gets.split
    kind = line[0] == 0 # true: 単離, false: 複利
    interest = line[1].to_f
    fee = line[2].to_i

    if kind
      a = 0
      year.times do
        a += (m * interest).to_i
        m -= fee
      end
      m += a
    else
      year.times do
        m += (m * interest).to_i
        m -= fee
      end
    end

    if max < m
      max = m
    end
  end

  puts max
end