Problems
Alice is trapped in Wonderland's labyrinth, being chased by the Queen of Hearts and her
herald! The labyrinth is a set of
Alice and the Queen of Hearts take turns making moves, and each knows the location of the other at all times. A move (by either of them) consists of either staying at the current junction or moving to another one that is connected to it by a corridor.
The Queen's herald, however, announces the next move the Queen makes in advance. That means that before anyone makes a move, he announces the Queen's first move. Then, Alice moves first. Then, each time the Queen moves, she must respect the previous announcement, and then decide her next move so the herald can announce it. Alice hears the announcements, so she always knows the Queen's next move before making her own.
If Alice and the Queen are at the same junction after either of them moves, then Alice is caught.
Otherwise, the pursuit continues. After
Alice chooses her moves optimally to escape. If she cannot escape, she chooses her moves to maximize the total number of moves until she is caught. The Queen chooses her moves optimally to try to catch Alice in as few total moves as possible.
Given the labyrinth's layout and the initial locations of both the Queen and Alice, find out whether Alice will be caught by the Queen and, if so, in how many moves.
Input
The first line of the input gives the number of test cases,
Output
For each test case, output one line containing Case #,
where SAFE
if Alice can avoid being caught for
Example
4
5 5 5 1
1 2
1 3
2 4
3 4
4 5
5 5 5 2
1 2
1 3
2 4
3 4
4 5
3 1 2 3
1 3
2 1 1 2
1 2
Case #1: SAFE
Case #2: 4
Case #3: SAFE
Case #4: 2