def tanri args
    amt, year, rate, fee = args
    cum = 0
    year.times{
        cum += (amt * rate).to_i
        amt -= fee.to_i
    }
    return amt + cum
end

def fukuri args
    amt, year, rate, fee = args
    year.times{amt = amt + (amt * rate).to_i - fee.to_i}
    return amt
end

m = gets.to_i
m.times{
    amt = gets.to_i
    year = gets.to_i
    n = gets.to_i
    arr = []
    n.times{
        type, rate, fee = gets.chomp.split.map(&:to_f)
        args = [amt, year, rate, fee]
        arr << (type == 0.0 ? tanri(args) : fukuri(args))
    }
    puts arr.sort.last
}
