Checker Board
Time limit1sMemory limit128 MB
Each row holds at most one checker per color; players slide their pieces along rows and the one who cannot move loses. Decide whether White wins, Black wins, or the game can run forever.
- Level
Hard9 of 10
- Topics
- Game theory, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
After capturing Gandalf, Bilbo, and the dwarves as they tried to make their way through the Misty Mountains, the Great Goblin makes Gandalf an offer: the two of them play a game the goblin has devised. If Gandalf wins, everyone goes free; if the Great Goblin wins, Gandalf must help him recover the treasures of Moria. Your task is to help Gandalf decide whether he can win.
The game is played on a rectangular checkerboard (its size varies) that holds white and black checkers — one color for each player — in a given, arbitrary arrangement. In each row there is at most one white checker and at most one black checker. Starting with the white player, the two players take turns sliding their checkers. On a turn a player picks one of their own checkers and slides it along its row (never between rows) to any empty cell; a checker may not jump over another checker, nor slide off the board.
The player who cannot move on their turn loses.
For example, consider the arrangement on the left below. On the first move White can slide the white checker in the top row all the way to the right, leaving the second player with no move, and wins. In the second game, although the position looks very favorable for White, White loses: every move White makes hands Black a reply, and eventually all of White's checkers get blocked against the far (left) wall.
Input
The first line contains the number of test cases (). The test cases follow one after another.
Each test case begins with a line of four integers (the number of rows), (the number of columns), (the number of white checkers), and (the number of black checkers), where and . The next lines describe the white checkers: the -th of them contains two integers and (, ), meaning the -th white checker sits in row , column . The following lines describe the black checkers in the same format.
Output
For each test case, print one line:
Wif the white player can win with best play,Bif the other player can win with best play,Tif the game can continue forever, with neither player able to force the other to lose.

