// Aizu 1135: Ohgas' Fortune
// 2017.9.13 bal4u@uu

#include <stdio.h>

int main()
{
	int m, i, j, a0, a, b, c, y, n, k, f, ans;
	double r;

	scanf("%d", &m);
	while (m-- > 0) {
		scanf("%d%d%d", &a0, &y, &n);
		for (ans = 0, i = 0; i < n; i++) {
			scanf("%d%lf%d", &k, &r, &f);
			if (k) {
				for (a = a0, j = 0; j < y; j++) b = (int)(a * r), a += b - f;
				if (a > ans) ans = a;
			} else {
				for (c = 0, a = a0, j = 0; j < y; j++) b = (int)(a * r), a -= f, c += b;
				a += c;	if (a > ans) ans = a;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}