Under construction

Still being built. Things may change

Rigged ㄱ Bomb Game

Time limit1sMemory limit256 MB

Summary
Boards hold A, B, and C bombs, and each query asks who wins the combined partizan game on a range of boards after swapping A and B on K of them.
Level

Hard9 of 10

Topics
Game theory, Segment tree, Math
Solved
No attempts yet

Rigged ㄱ Bomb Game

Ahgus and Bagus are playing the ㄱ Bomb Game.

The ㄱ Bomb Game is played on NN boards. The ii-th board is a rectangle with width and height of RiR_i and CiC_i, respectively, made of 1×1 square cells.

Each cell holds one ㄱ bomb, and each bomb is one of three types: A, B, or C. Only Ahgus can choose an A bomb, only Bagus can choose a B bomb, and nobody can choose a C bomb.

When an ㄱ bomb explodes, it ignites the bombs to its left and below. An ignited bomb explodes right away and ignites the bombs to its own left and below. For example, if you detonate the bomb in the top-right cell of a board, every bomb on that board explodes. When the chain ends and no more bombs explode, the turn of the current player is over.

Ahgus always goes first. The two players alternate turns, and on each turn a player detonates one of the bombs they are allowed to choose. The player who has no bomb left to detonate on their turn loses.

Both players swapped some bombs to gain an advantage. On every board, there is exactly one bomb of type A or B, or of both types combined.

A stonejjun, an alchemist, appeared and proposed playing TT games. In each game, he selects KK boards and swaps the A and B bombs on each of them. He also chooses two integers UU and VV and hides every board except those numbered from UU to VV inclusive, so only those boards can be used.

Angered by this, Ahgus and Bagus decide to determine the winner without playing. If both players make the best possible choices, determine who wins each game.

Input

The first line contains NN.

For each of the NN boards, one line contains two positive integers RiR_i and CiC_i. Below that, RiR_i lines of CiC_i characters each describe the ii-th board, starting from the top-left cell.

The next line contains TT, the number of games. Each of the following TT lines contains three positive integers KK, UU, and VV.

Output

For each of the TT games, print the winner, either Ahgus or Bagus, on its own line.

Constraints

  • 1≤N≤30,0001 \le N \le 30,000
  • 1≤Ri,Ci,Ri×Ci≤401 \le R_i, C_i, R_i \times C_i \le 40
  • 1≤T≤100,0001 \le T \le 100,000
  • 1≤K≤N1 \le K \le N
  • 1≤U≤V≤N1 \le U \le V \le N
  • Every character on every board is A, B, or C. On each board, exactly one character is A or B, or the board has one bomb of each kind that is counted as a single bomb.

Examples1

  1. Example 1

    Input
    5
    1 1
    A
    2 2
    AB
    AA
    1 3
    ACB
    2 4
    CCAC
    BAAA
    1 1
    B
    5
    2 2 3
    5 1 1
    4 1 5
    2 1 3
    5 5 5
    
    Expected output
    Bagus
    Ahgus
    Bagus
    Ahgus
    Bagus