#include<cstdio>
bool fun(int n){
	return n % 400 == 0 || n % 100 != 0 && n % 4 == 0;
}
int main(){
	int n;
	int mday[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int total[13] = { 0 };
	for (int i = 2; i < 13; i++){
		total[i] = total[i - 1] + mday[i - 1];
	}
	while (scanf("%d", &n) != EOF){
		for (int i = 0; i < n; i++){
			int year, month, day, totalday;
			scanf("%d%d%d", &year, &month, &day);
			if (fun(year) && month>2){
				totalday = total[month] + day + 1;
			}
			else{
				totalday = total[month] + day;
			}
			printf("%d\n", totalday);
			}
	}
}