#include <stdio.h>

int main(void) {
	int i, m, n, initial_fund, year, flg, dec, rst, max, sum;
	double per;

	scanf("%d", &m);
	while (m--) {
		scanf("%d%d%d", &initial_fund, &year, &n);
		max = 0;
		while (n--) {
			scanf("%d%lf%d", &flg, &per, &dec);
			rst = initial_fund;
			for (i = 0, sum = 0; i < year; i++) {
				if (flg == 1) {
					rst = rst + (int)(rst * per) - dec;
				} else {
					sum = sum + (int)(rst * per);
					rst = rst - dec + sum;
				}
			}
			if (rst > max) {
				max = rst;
			}
		}
		printf("%d\n", max);
	}
	return 0;
}