### main

m = gets.to_i

m.times do
  fund = gets.to_i
  year = gets.to_i

  max_fund = 0
  n = gets.to_i

  n.times do
    line = gets.strip.split
    inti = Rational(line[1])
    costi = line[2].to_i
    #p [flagi, inti, costi]

    if line[0] == '0'	# Simple Interest
      f = fund
      sumi = 0

      year.times do
        sumi += (f * inti).truncate
        f -= costi
      end
      f += sumi

      max_fund = f if max_fund < f
    else		# Compound Interest
      f = fund

      year.times do
        f += (f * inti).truncate - costi
      end

      max_fund = f if max_fund < f
    end
  end

  puts max_fund
end