Under construction

Still being built. Things may change

Magic Clock (Basic)

Time limit2sMemory limit1024 MB

Summary
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 60⋅12=72060 \cdot 12 = 720 divisions, numbered from 00 to 719719 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 112\frac{1}{12} 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 mm and the hour hand points to hh, and the distance between them is d=(h−m) mod 720d = (h - m) \bmod 720 divisions, then if 0<d<120 < d < 12, at the end of the next minute the minute hand is at division 00. The hour hand keeps moving normally.

Heimerdinger proposed qq theories about the nature and abilities of such clocks. To check the ii-th theory, you must find how many minutes it takes for the hands to go from state si,1s_{i,1} to state si,2s_{i,2}.

Input

The first line contains a single integer qq, the number of queries (1⩽q⩽20001 \leqslant q \leqslant 2000). Each of the next qq lines contains four integers h1h_1, m1m_1, h2h_2, and m2m_2, separated by spaces. They are the divisions that the hour and minute hands point to in the initial and final states, respectively (0⩽h1,2,m1,2<7200 \leqslant h_{1,2}, m_{1,2} < 720).

Output

Print qq lines, one answer per query. The answer is the minimum number of minutes after which the clock goes from state (h1,m1)(h_1, m_1) to state (h2,m2)(h_2, m_2), or -1 if the clock never reaches the second state when starting from the first.

Examples2

  1. Example 1

    Input
    4
    0 0 1 12
    0 0 60 0
    11 0 12 0
    12 0 13 0
    
    Expected output
    1
    60
    1
    -1
    
  2. Example 2

    Input
    5
    133 0 415 0
    5 717 87 36
    193 0 230 0
    537 333 28 48
    459 76 2 0
    
    Expected output
    1722
    82
    757
    -1
    -1