fun main(args: Array<String>?): Unit {
    repeat(readLine()!!.trim().toInt()) {
        val initial = readLine()!!.trim().toInt()
        val year = readLine()!!.trim().toInt()
        var maxCount = initial
        repeat(readLine()!!.trim().toInt()){
            val arg = readLine()!!.trim().split(' ')
            val type = arg[0].toInt()
            val r = arg[1].toDouble()
            val fee = arg[2].toInt()
            val result = when(type) {
                0 -> {
                    var baseMoney = initial
                    var gain = 0
                    repeat(year) {
                        gain += (baseMoney * r).toInt()
                        baseMoney -= fee
                    }
                    baseMoney + gain
                }
                1 -> {
                    var money = initial
                    repeat(year) {
                        money += (money * r).toInt() - fee
                    }
                    money
                }
                else -> TODO()
            }
            if (result > maxCount) maxCount = result
        }
        println(maxCount)
    }
}

