This page is still under construction.

Parts of this page are still being built. What you see may change.

Time Warp

Time limit3sMemory limit256 MB

Summary
Given phrases like 'a after b', find the nearest clock time after or before the hour when the directed hand angle equals a.
Level

Medium5 of 10

Topics
Math
Solved
No attempts yet

Problem

Tim tells the time in his own way. Ask him and he might answer 20 after 8, which sounds ordinary, or 90 after 8 and 126 til 4, which do not.

Tim's rule is this. 20 after 8 is the first time after 8 o'clock at which the hour hand and the minute hand make an angle of 20 degrees. 126 til 4 is the time closest to 4 o'clock among the times before 4 o'clock at which the two hands make an angle of 126 degrees.

Both hands move continuously at a constant speed. Write a program that converts Tim's phrases into ordinary times.

Input

The first line has the number of test cases nn. Each of the next nn lines holds one string of the form a after b or a til b, where aa and bb are integers with 0≤a<3600 \le a < 360 and 1≤b≤121 \le b \le 12.

The angle is measured starting at the hour hand and moving clockwise until reaching the minute hand. At 9 o'clock the hands make an angle of 90 degrees, and at 3 o'clock they make an angle of 270 degrees.

after means only a time later than bb o'clock, and til means only a time earlier than bb o'clock. If the angle is already aa degrees at bb o'clock, that exact moment is not an answer.

Output

For each test case print one line of the form Case i: h:m:s, where ii is the test case number starting at 1, and hh, mm and ss are the hour, the minute and the second closest to the time you found.

The hour satisfies 1≤h≤121 \le h \le 12 and is printed without padding. Always print two digits for the minute and two digits for the second.

Examples2

  1. Example 1

    Input
    4
    20 after 8
    126 til 4
    180 til 1
    0 after 12
    
    Expected output
    Case 1: 8:47:16
    Case 2: 3:39:16
    Case 3: 12:32:44
    Case 4: 1:05:27
    
  2. Example 2

    Input
    1
    0 after 1
    
    Expected output
    Case 1: 1:05:27