// Arup Guha
// 7/23/08
// Solution to BHCSI contest problem: Late

import java.util.*;
import java.io.*;

public class late {
	
	final static int SNOOZE_TIME = 9;
	final static int CLASS_TIME = 9*60 + 30;
	
	public static void main(String[] args) throws IOException {
		
		Scanner fin = new Scanner(new File("late.in"));
		
		int numStudents = fin.nextInt();
		
		
		for(int i=1; i<=numStudents; i++) {
			
			int typeStudent = fin.nextInt();
			int timeExtra = 0;
			
			// If we live in the dorm, add 9 minutes per extra snooze.
			if (typeStudent == 0) {
				int snooze = fin.nextInt();
				timeExtra += (SNOOZE_TIME*(snooze-1));	
			}
			
			// If we are a commuter, add 0, 10 or 20 minutes depending on
			// breakfast. I was too lazy to do the if and did a nify formula
			// to relate the choice number to the number of minutes to add.
			// Normally, this is a horrible programming practice, since the
			// choices could easily change!!!
			else {
				int breakfast = fin.nextInt();
				timeExtra += (10*(3-breakfast));
			}
			
			// Add in time to get to class.
			timeExtra += 20;
			
			int h = fin.nextInt();
			int m = fin.nextInt();
			
			// My Linkin Park tribute =)
			int minutesPastMidnight = h*60 + m;
			minutesPastMidnight += timeExtra;
			int howLate = minutesPastMidnight - CLASS_TIME;
			
			// Output whether or not the student is late!
			if (howLate > 0)
				System.out.println("Student #"+i+" arrives to class "+howLate+" minutes late.");
			else
				System.out.println("Student #"+i+" arrives to class on time.");
		}
		
		fin.close();
	}
}