Game

Interview

Time limit1sMemory limit128 MB

Summary
Given many filled 5x5 tic-tac-toe boards, determine for each whether A, B, or neither has three consecutive same marbles in a row, column, or diagonal.
Level

Easy3 of 10

Topics
Matrix, Simulation, Implementation
Solved
No attempts yet

Problem

Tic-tac-toe is a favorite way to pass a lazy afternoon in Ardenia. Arthum and Breece are not fans of the game, but their mother told them to play, so they sit down at a 5 × 5 board. Each of them owns a large pile of marbles: Arthum's marbles are marked with an A, and Breece's are marked with a B.

Because they are only two years old, they have no concept of taking turns. Instead, they toss their marbles onto the board as fast as they can, so after a while every one of the 25 fields holds either an A marble or a B marble.

Now they would like to know who won, but counting is not their strong suit either. In tic-tac-toe a player wins by getting three of their own marbles in a row — three consecutive fields lined up horizontally, vertically, or diagonally (in either diagonal direction). A run of more than three marbles also counts, since it contains three consecutive marbles.

  • If only Arthum has three marbles in a row, Arthum wins.
  • If only Breece has three marbles in a row, Breece wins.
  • If both of them have three in a row, or neither of them does, the game is a draw.

The input describes several finished games. The first line contains an integer ZZ (1≤Z≤1000001 \le Z \le 100000), the number of games, followed by the ZZ games in the format described below.

Input

The first line contains the number of games ZZ. Each game is described by 5 lines, and each of those lines contains exactly 5 characters, each either A or B, describing one 5 × 5 board.

Output

For each game, output a single line containing its result: A wins, B wins, or draw.

Examples4

  1. Example 1

    Input
    2
    AABBA
    BAAAB
    AAABA
    ABAAB
    BAAAB
    AAAAA
    AAAAA
    BAAAA
    ABAAA
    AABAA
    
    Expected output
    A wins
    draw
    
  2. Example 2

    Input
    1
    AAAAA
    AAAAA
    AAAAA
    AAAAA
    AAAAA
    
    Expected output
    A wins
    
  3. Example 3

    Input
    1
    BBBBB
    BBBBB
    BBBBB
    BBBBB
    BBBBB
    
    Expected output
    B wins
    
  4. Example 4

    Input
    1
    AABBA
    BBAAB
    AABBA
    BBAAB
    AABBA
    
    Expected output
    draw