Problem:
Millennium

A wise king declared a new calendar. "Tomorrow shall be the first day of the calendar, that is, the day 1 of the month 1 of the year 1. Each year consists of 10 months, from month 1 through month 10, and starts from a big month. A common year shall start with a big month, followed by small months and big months one after another. Therefore the first month is a big month, the second month is a small month, the third a big month, ..., and the 10th and last month a small one. A big month consists of 20 days and a small month consists of 19 days. However years which are multiples of three, that are year 3, year 6, year 9, and so on, shall consist of 10 big months and no small month."

Many years have passed since the calendar started to be used. For celebration of the millennium day (the year 1000, month 1, day 1), a royal lottery is going to be organized to send gifts to those who have lived as many days as the number chosen by the lottery. Write a program that helps people calculate the number of days since their birthdate to the millennium day.

Input

The input is formatted as follows.

n

Y1 M1 D1

Y2 M2 D2

...

Yn Mn Dn

Here, the first line gives the number of datasets as a positive integer n, which is less than or equal to 100. It is followed by n datasets. Each dataset is formatted in a line and gives three positive integers, Yi (< 1000), Mi (≤ 10), and Di (≤ 20), that correspond to the year, month and day, respectively, of a person's birthdate in the king's calendar. These three numbers are separated by a space.

Output

For the birthdate specified in each dataset, print in a line the number of days from the birthdate, inclusive, to the millennium day, exclusive. Output lines should not contain any character other than this number.

Sample Input

8
1 1 1
344 3 1
696 5 1
182 9 5
998 8 7
344 2 19
696 4 19
999 10 20

Output for the Sample Input

196470
128976
59710
160715
252
128977
59712
1

Solution in JAVA (judge verdict: Accepted)


import java.util.Scanner;

public class Main 
{	
	static int toMillenial(int startY, int startM, int startD)
	{
		int y = startY;
		int m = startM;
		int d = startD;
		int remy = 1000-startY;
		int remm = 0;
		int remd = 0;
		int ans = 0;
		
		//go to the first day of next month.
		if(d > 1)
		{
			if(y%3 == 0 || m%2 == 1)
			{ //big month
				ans += (21-d);
				d = 1;
			}
			else
			{ //small month
				ans += (20-d);
				d = 1;
			}
			m += 1;
			while(m>10)
			{
				m -= 10;
				y += 1;
			}
		}
//		System.out.println("Going to first day: " + ans);
//		System.out.println("It is now " + y + " " + m + " " + d);
		
		//go to the first month of next year.
		if(m > 1)
		{
			if(y%3 == 0)
			{ //big year
				ans += (11-m)*20;
				m = 1;
				y += 1;
			}
			else
			{ //common year
				while(m <= 10)
				{
					if(m%2 == 1)
					{ //big month
						ans += 20;
					}
					else
					{ //small month
						ans += 19;
					}
					m += 1;
				}
				m = 1;
				y += 1;
			}
		}
//		System.out.println("Going to first month: " + ans);
//		System.out.println("It is now " + y + " " + m + " " + d);
		
		while(y < 1000)
		{
			if(y%3 == 0)
			{ //big year
				ans += 200;
			}
			else
			{ //small year
				ans += 195;
			}
			y += 1;
		}
//		System.out.println("Going to year 1000: " + ans);
//		System.out.println("It is now " + y + " " + m + " " + d);
		
		return ans;
	}
	
	public static void main(String[] args) 
	{		
		//normal year: 195 days
		//big year: 200 days
//		System.out.println(19*5);
		
		Scanner scan = new Scanner(System.in);
		int ppl;
		ppl = scan.nextInt();
		int[] year = new int[ppl];
		int[] month = new int[ppl];
		int[] day = new int[ppl];		
		
		for(int i = 0; i < ppl; i++)
		{
			year[i] = scan.nextInt();
			month[i] = scan.nextInt();
			day[i] = scan.nextInt();
			
			System.out.println(toMillenial(year[i],month[i],day[i]));
		}
	}
	
}