This page is still under construction.

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

Hoof and Brain

Time limit4sMemory limit1024 MB

Summary
Given a directed graph and two starting tokens, decide for each query whether the brain or the hoof wins the token-moving game.
Level

Hard8 of 10

Topics
Graph, BFS
Solved
No attempts yet

Problem

Given a directed graph with NN vertices and MM edges (2≤N≤1052 \leq N \leq 10^5, 1≤M≤2⋅1051 \leq M \leq 2 \cdot 10^5), Farmer John's cows like to play the following game with two players.

Place two tokens on distinct nodes in the graph. Each turn, one player, the brain, chooses a token that must be moved along an outgoing edge. The other player, the hoof, chooses which edge the token moves along. The two tokens can never be on the same node. If at some point the hoof cannot make a valid move, the brain wins. If the game continues indefinitely, the hoof wins.

You are given QQ queries (1≤Q≤1051 \leq Q \leq 10^5) giving the starting nodes of the two tokens. For each query, output which player wins.

Input

The first line contains NN and MM.

The next MM lines each contain two integers aa and bb, denoting an edge from aa to bb.

The graph has no self-loops or multiple edges.

The next line contains QQ.

The final QQ lines each contain two integers xx and yy satisfying 1≤x,y≤N1\le x,y\le N and x≠yx\neq y, giving the starting nodes of the tokens.

Output

Output a string of length QQ. Each character is B if the brain wins and H if the hoof wins.

Hint

The brain can win the first game by selecting node 5. Then the hoof has no valid move.

The brain can win the last game by selecting node 4 and then node 7. Then the hoof has no valid move.

The hoof wins the other games.

Examples1

  1. Example 1

    Input
    9 10
    1 2
    2 3
    3 4
    4 7
    3 5
    1 6
    6 8
    8 9
    9 6
    7 2
    4
    1 5
    1 2
    1 6
    2 4
    
    Expected output
    BHHB