Virtual Rabbit (Small)

Time limit5sMemory limit512 MB

Summary
Feed a pet at allowed times of day so no gap between feedings exceeds X seconds, using as few feedings as possible.
Level

Medium5 of 10

Topics
Greedy, Intervals
Solved
No attempts yet

Problem

Alice just bought a virtual pet rabbit. The rabbit hops around on the screen and eats on the spot whenever she presses a button. Alice is fond of the rabbit, but she is busy and does not want to spend much time taking care of it. If the rabbit goes without food for too long, it dies and Alice loses the game.

Every day Alice gets up at time GG, leaves for work at time WW, comes back home at time HH, and goes to bed at time BB. She cannot feed the rabbit while she is at work or asleep, that is, during the intervals [W,H)[W, H) and [B,G)[B, G). The times WW and BB are not valid feeding times, while the times HH and GG are. In every other second Alice either presses the button and feeds the rabbit instantly, or does nothing.

The rabbit dies once more than XX seconds have passed since it last ate.

It is now 00:00:00 on day 0, and the mail carrier has just delivered the rabbit. The carrier presses the button at 00:00:00 even though Alice is asleep, and then leaves. Alice wants the rabbit to be alive at 00:00:00 on day DD. If she can keep it alive, how few times can she feed it?

Stated precisely: number every second from 0, so that 00:00:00 on day 0 is second 0 and 00:00:00 on day dd is second 86400d86400d. Let f1<f2<⋯<fnf_1 < f_2 < \dots < f_n be the seconds at which Alice feeds the rabbit, and let f0=0f_0 = 0 be the moment the mail carrier pressed the button. The rabbit is alive at 00:00:00 on day DD exactly when both conditions hold.

  • fi−fi−1≤Xf_i - f_{i-1} \le X for every ii with 1≤i≤n1 \le i \le n
  • 86400D−fn≤X86400D - f_n \le X

Every fif_i with i≥1i \ge 1 must be a second whose time of day falls in [G,W)[G, W) or in [H,B)[H, B). Find the smallest possible nn.

Input

The first line contains the number of test cases TT. Then TT test cases follow, each consisting of 6 lines. The first five lines give the times GG, WW, HH, BB and the length XX in "hh:mm:ss" format, one per line. The last line contains one integer DD.

Limits

  • 1≤T≤1001 \le T \le 100
  • Alice always goes to bed before midnight and gets up after midnight, so GG, WW, HH and BB increase strictly within the same day.
  • 00:00:00≤G<W<H<B≤23:59:59\text{00:00:00} \le G < W < H < B \le \text{23:59:59}
  • 00:00:00<X≤23:59:59\text{00:00:00} < X \le \text{23:59:59}
  • 1≤D≤10001 \le D \le 1000

Output

For each test case print one line in the format "Case #x: y", where xx is the test case number starting from 1 and yy is the minimum number of times Alice has to feed the rabbit. If there is no way to keep the rabbit alive at 00:00:00 on day DD, then yy is −1-1.

Hint

In the first test case of the example, Alice can feed the rabbit at 08:00:00 and at 20:00:00 every day.

In the second test case of the example, the rabbit dies before Alice even wakes up on day 0.

Examples1

  1. Example 1

    Input
    3
    08:00:00
    09:00:00
    18:00:00
    22:00:00
    12:00:00
    100
    08:00:00
    09:00:00
    18:00:00
    22:00:00
    01:00:00
    1
    00:00:00
    12:00:00
    12:00:01
    23:59:59
    00:00:02
    2
    
    Expected output
    Case #1: 200
    Case #2: -1
    Case #3: 86401