Magic Clock (Basic)
Time limit2sMemory limit1024 MB
Given a 720-division clock whose minute hand teleports to 0 when it catches the hour hand, find the minutes needed to go from one state to another, or -1 if unreachable.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
Scientists from Piltover obtained a very mysterious magic clock. The strange movement of its hands made them suspect that important information may be encrypted in it.
The clock has a dial of divisions, numbered from to clockwise, plus a minute hand and an hour hand. Every minute, the minute hand moves 12 divisions and the hour hand moves 1. So on an ordinary clock, in one hour (60 minutes), the minute hand goes around the full dial once, and the hour hand moves of the circle.
The clock is unusual in one way. When on some tick the minute hand must overtake or catch up with the hour hand, it teleports to the start. Specifically, if the minute hand points to division and the hour hand points to , and the distance between them is divisions, then if , at the end of the next minute the minute hand is at division . The hour hand keeps moving normally.
Heimerdinger proposed theories about the nature and abilities of such clocks. To check the -th theory, you must find how many minutes it takes for the hands to go from state to state .
Input
The first line contains a single integer , the number of queries (). Each of the next lines contains four integers , , , and , separated by spaces. They are the divisions that the hour and minute hands point to in the initial and final states, respectively ().
Output
Print lines, one answer per query. The answer is the minimum number of minutes after which the clock goes from state to state , or -1 if the clock never reaches the second state when starting from the first.