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.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);

	public static void main(String[] args) {
		new A1179().ans(sc);

	}

}

class A1500 {
	int count1500 = 0;

	public void ans(Scanner sc) {
		int len = sc.nextInt();
		sc.nextLine();
		String id = sc.nextLine();
		int x = sc.nextInt();
		ArrayList<Integer> pa = new ArrayList<Integer>();
		ArrayList<Integer> po = new ArrayList<Integer>();
		for (int i = 0; i < x; i++) {
			pa.add(sc.nextInt());
			if (pa.get(i) % 2 == 0) {

			} else {
				po.add(pa.get(i));
			}
		}

		int sum = 0;
		int even = 0;
		int odd = 0;

		for (int i = 0; i < id.length(); i++) {
			int t = id.length() - i - 1;

			char c = id.charAt(t);
			if (c == '*') {
				if (t % 2 == 0) {
					even++;
				} else {
					odd++;
				}
			} else {
				int d = Integer.parseInt(String.valueOf(c));
				if (t % 2 == 0) {
					sum += (d / 10) + (d % 10);
				} else {
					sum += d;
				}
			}
		}

		System.out.println(sum);
		System.out.println(even);
		System.out.println(odd);

		System.out.println(id);
	}

	void re1500(int sum, int even, int odd, ArrayList<Integer> pa,
			ArrayList<Integer> po) {
		if (even == 0 && odd == 0) {

		} else {
			if (sum % 2 == 0) {

			} else {
				if (odd > 0) {
					for (Iterator iterator = po.iterator(); iterator.hasNext();) {
						Integer integer = (Integer) iterator.next();
						re1500(sum + integer, even, odd - 1, pa, po);
					}
				}
			}
		}
	}

}

class A1129 {

	public void ans(Scanner sc) {

		while (true) {
			int n = sc.nextInt();
			int r = sc.nextInt();

			if (n == 0) {
				break;
			}
			LinkedList<Integer> yama = new LinkedList<Integer>();
			for (int i = 0; i < n; i++) {
				yama.addFirst(i + 1);
			}
			for (int i = 0; i < r; i++) {
				int p = sc.nextInt();
				int c = sc.nextInt();

				for (int j = 0; j < c; j++) {
					yama.add(j, yama.remove(p + j - 1));
				}
			}
			System.out.println(yama.getFirst());
		}

	}

}

class A1135 {

	public void ans(Scanner sc) {
		int M = sc.nextInt();

		for (int m = 0; m < M; m++) {
			int money = sc.nextInt();
			int year = sc.nextInt();

			int n = sc.nextInt();
			int max = 0;
			for (int i = 0; i < n; i++) {
				int me = sc.nextInt();
				double ri = sc.nextDouble();
				int te = sc.nextInt();

				int A = money;
				if (me == 1) {
					for (int y = 0; y < year; y++) {
						A = A + (int) (A * ri) - te;
					}
				} else {
					int Risi = 0;
					for (int y = 0; y < year; y++) {
						Risi += (int) (A * ri);
						A = A - te;
					}
					A += Risi;
				}
				max = Math.max(max, A);
			}

			System.out.println(max);
		}

	}

}

class A1159 {

	public void ans(Scanner sc) {
		while (true) {
			int N = sc.nextInt();
			int P = sc.nextInt();

			LinkedList<Integer> member = new LinkedList<Integer>();

			if (N == 0) {
				break;
			}
			for (int i = 0; i < N; i++) {
				member.add(0);
			}

			int i = 0;
			int p = P;
			while (true) {

				if (0 < p) {
					member.set(i, member.get(i) + 1);
					p--;
					if (member.get(i) == P) {
						break;
					}
				} else {
					p += member.get(i);
					member.set(i, 0);
				}
				i = (i + 1) % member.size();
			}

			System.out.println(i);
		}

	}

}

class A1165 {

	public void ans(Scanner sc) {
		while (true) {
			int N = sc.nextInt();

			LinkedList<Integer> xl = new LinkedList<Integer>();
			LinkedList<Integer> yl = new LinkedList<Integer>();

			if (N == 0) {
				break;
			}

			xl.add(0);
			yl.add(0);

			for (int i = 1; i < N; i++) {
				int n = sc.nextInt();
				int a = sc.nextInt();

				int dx = 0;
				int dy = 0;
				switch (a) {
				case 0:
					dx = -1;
					break;
				case 1:
					dy = -1;
					break;
				case 2:
					dx = 1;
					break;
				case 3:
					dy = 1;
					break;

				}
				xl.add(xl.get(n) + dx);
				yl.add(yl.get(n) + dy);
			}

			int xx = Collections.max(xl);
			int yx = Collections.max(yl);
			int xs = Collections.min(xl);
			int ys = Collections.min(yl);

			System.out.printf("%d %d\n", xx - xs + 1, yx - ys + 1);
		}

	}

}

class A1179 {

	public void ans(Scanner sc) {
		int N = sc.nextInt();
		int n = 0;
		while (n < N) {
			n++;

			int Y = sc.nextInt();
			int M = sc.nextInt();
			int D = sc.nextInt();

			int count = 0;
			for (int y = Y + 1; y < 1000; y++) {
				if (y % 3 == 0) {
					count += (20 * 10);
				} else {
					count += (20 * 5 + 19 * 5);
				}
			}
			for (int m = M + 1; m <= 10; m++) {
				if (Y % 3 == 0) {
					count += 20;
				} else {
					if (m % 2 == 0) {
						count += 19;
					} else {
						count += 20;
					}
				}
			}
			if (M % 2 == 0 && Y % 3 != 0) {
				count += (19 - D);

			} else {
				count += (20 - D);
			}

			System.out.printf("%d\n", count + 1);
		}

	}

}