#include<iostream>
using namespace std;
//四年一润,百年不润,四百年再润,例如思考2000年,2100年,2400年与2500年,就理解了能被4整除但不能被100整数了
int m_1[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
int m_2[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main(void)
{
int m;
while(cin >> m)
{
while(m--)
{
int day_gross = 0;
int year,month,day;
cin >> year >> month >> day;
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
{
for(int i = 0;i < month - 1;i++)
day_gross += m_1[i];
day_gross += day;
cout << day_gross << endl;
}
else
{
for(int i = 0;i < month - 1;i++)
day_gross += m_2[i];
day_gross += day;
cout << day_gross << endl;
}
}
}
return 0;
}