This page is still under construction.

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

Triangle War

Time limit1sMemory limit128 MB

Summary
From a partially played Triangle War position, decide with perfect play which player ends up owning more of the 9 small triangles.
Level

Hard8 of 10

Topics
Game theory, Backtracking, Bit manipulation, Simulation
Solved
No attempts yet

Problem

Triangle War is a two-player game played on the following triangular grid:

Two players, A and B, take turns filling in any dotted line connecting two dots, with A going first. Once a line is filled, it cannot be filled again. If the line a player fills completes one or more triangles, that player owns those triangles and takes another turn (the opponent's turn is skipped). The game ends once all dotted lines are filled, and the player who owns the most triangles wins. The size of the margin does not matter.

For example, suppose A fills in the line between 2 and 5 in the partial game on the left below:

Then A owns the triangle labelled A and takes another turn, filling in the line between 3 and 5. B can now claim 3 triangles (if desired) by filling in the line between 2 and 3, then the line between 5 and 6, and finally the line between 6 and 9; B would then take one more move before it becomes A's turn again.

In this problem you are given a number of moves that have already been made. From the partial game, determine which player wins, assuming both players play perfectly from that point on — that is, each player always chooses the move that leads to the best possible outcome for themselves.

Input

The first line of input is a positive integer, the number of games that follow. Each game begins with an integer mm (6≤m≤186 \le m \le 18), the number of moves already made in that game. The next mm lines each describe a move in order, in the form i j (with i<ji < j), meaning the line between dots ii and jj is filled on that move. You may assume every given move is legal.

Output

For each game, print one line: Game k: A wins. if A wins, or Game k: B wins. if B wins, where k is the game's number (the first game is 1).

Examples1

  1. Example 1

    Input
    4
    6
    2 4
    4 5
    5 9
    3 6
    2 5
    3 5
    7
    2 4
    4 5
    5 9
    3 6
    2 5
    3 5
    7 8
    6
    1 2
    2 3
    1 3
    2 4
    2 5
    4 5
    10
    1 2
    2 5
    3 6
    5 8
    4 7
    6 10
    2 4
    4 5
    4 8
    7 8
    
    Expected output
    Game 1: B wins.
    Game 2: A wins.
    Game 3: A wins.
    Game 4: B wins.